ios - recorrer - tamaño de un objeto javascript



¿Cómo verificar los mismos valores para las múltiples claves en el conjunto de objetos json? (1)

Estoy trabajando en el calendario usando Swift3 . Sé que es posible establecer los mismos valores para diferentes claves.

Tengo este NSArray llamado Dictionary de objetos JSON :

if parsedData["status"] as! Int == 200 {

    if let Streams = parsedData["data"] as! [AnyObject]? {
       for Stream in Streams {
           print(Stream) 
       }



Stream result: {
"-" = 4;
1 = X;
10 = H;
11 = X;
12 = X;
13 = X;
14 = X;
15 = X;
16 = X;
17 = H;
18 = X;
19 = X;
2 = X;
20 = X;
21 = X;
22 = X;
23 = L;
24 = H;
25 = X;
26 = L;
27 = "-";
28 = "-";
29 = "-";
3 = H;
30 = "-";
4 = X;
5 = X;
6 = X;
7 = X;
8 = X;
9 = X;
H = 4;
L = 2;
X = 20;
blank = "";
classid = id;
classname = " (A)";
stdid = 1;
stdnm = "name"; }

Aquí 1 a 31 son constantes días de dependen de mes. XHL son hojas, hojas normales y normales. Necesito verificar los valores x, h, l y obtener fechas.

Sin embargo, no conozco exactamente la lógica para verificar si los valores son los mismos, luego agrego su valor a la misma clave.

Por favor, ayúdame. Agradeciendote


Por favor, compruebe :

Esto es para tu formato JSON

if let streams = parsedData["data"] as! [AnyObject]? {
    for stream in streams {
        let dict = stream as? [AnyHashable: Any]
        print(getValue(key: "H", dict: dict!))
        print(getValue(key: "L", dict: dict!))
        print(getValue(key: "X", dict: dict!))
    }
}

func getValue(key: String, dict: [AnyHashable: Any]) -> [Int] {
    return dict.filter { $0.value as? String == key }.flatMap { Int(String(describing: $0.key)) }
}

Debajo está para los diccionarios

let dict = ["-": 4, 1: "X", 10: "H", 11: "X", 12: "X", 13: "X", 14: "X",
           15: "X", 16: "X", 17: "H", 18: "X", 19: "X", 2: "X", 20: "X",
           21: "X", 22: "X", 23: "L", 24: "H", 25: "X", 26: "L", 27: "-",
           28: "-", 29: "-", 3: "H", 30: "-", 4: "X", 5: "X", 6: "X", 7: "X",
           8: "X", 9: "X", "H": 4, "L": 2, "X": 20] as [AnyHashable : Any]

Opción 1 Pase la clave y devolverá la matriz de fechas

    print(getValue(key: "H", dict: dict)) //[17, 10, 24, 3]
    print(getValue(key: "L", dict: dict)) //[26, 23]
    print(getValue(key: "X", dict: dict)) //[12, 25, 14, 20, 5, 15, 7, 11, 13, 16, 19, 22, 21, 9, 18, 2, 6, 4, 1, 8]

    func getValue(key: String, dict: [AnyHashable: Any]) -> [Int] {
        return dict.filter { $0.value as? String == key }.flatMap { $0.key as? Int}
    }

Opción 2: pasar varias teclas y devolverá el diccionario con la matriz de fechas asociada a cada tecla

    print(getValue2(keys: ["H","L","X"], dict: dict))
    //["H": [17, 24, 3, 10], "L": [23, 26], "X": [14, 20, 13, 19, 21, 9, 6, 1, 12, 25, 5, 15, 7, 11, 16, 22, 18, 2, 4, 8]]

    func getValue2(keys: [String], dict: [AnyHashable: Any]) -> [String: [Int]] {
        var returnValue = [String: [Int]]()
        for key in keys {
            returnValue[key] = [Int]()
        }

        dict.forEach {
            if let v = $0.value as? String, returnValue[v] != nil, let k = $0.key as? Int {
                returnValue[v]!.append(k)
            }
        }
        return returnValue
    }




calendar