Map value in particular key in array of dictionary

2020-05-02 08:51发布

Map value the particular key wherever presents in array of dictionary and replace it in same array.

We need to update pan_card key 0 to 1, in occurence of array of dictionary.

let keyToUpdate = "pan_card"
var arrayOfDictionary = [[String:Any]]()
var firstDict = [String:Any]()
firstDict["passport"] = 0
firstDict["ration_card"] = 0
firstDict["pan_card"] = 0

var arrayDict = [String : Any]()
arrayDict["currentObject"] = firstDict
arrayDict["title"] = "Documents list"

var secondDict = [String:Any]()
secondDict["dl"] = 0
secondDict["voter"] = 0
secondDict["pan_card"] = 0

//let dic = secondDict.filter({ $0.value as! NSNumber != 0})
//secondDict = dic
//print(secondDict)
//let dictionary = ["foo": 1, "bar": 2, "baz": 5]
//
//let newDictionary = dictionary.mapValues { value in
//    return value - value
//}
//print(dictionary)
//print(newDictionary)
var arrayDict2 = [String : Any]()
arrayDict2["currentObject"] = secondDict
arrayDict2["title"] = "Second Documents list"


arrayOfDictionary.append(arrayDict)
arrayOfDictionary.append(arrayDict2)
//print(arrayOfDictionary)



    for (index, dictionary) in arrayOfDictionary.enumerated() {
    let dict = dictionary
    let newDictionary = (dict["currentObject"] as![String:Any]).mapValues { value in
            return 1
        }
    arrayOfDictionary[index] = newDictionary
}
print(arrayOfDictionary)

This code updating every key in currentObject

and tried this as well, but it adding new key

for (index, dictionary) in arrayOfDictionary.enumerated() {
    var dict = dictionary
//    let newDictionary = (dict["currentObject"] as![String:Any]).mapValues { value  in
//            return 1
//        }
    var newDictionary = [String: Any]()
    for (key, value) in dict["currentObject"] as![String:Any] {
        dict[keyToUpdate, default: value] = 1
    }
        arrayOfDictionary[index] = dict
}
print(arrayOfDictionary)

I need output like below

Original value

[["currentObject": ["passport": 0, "pan_card": 0, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 0, "dl": 0, "voter": 0], "title": "Second Documents list"]] 

after update

[["currentObject": ["passport": 0, "pan_card": 1, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 1, "dl": 0, "voter": 0], "title": "Second Documents list"]] 

Referred Document Link

We knows manually iterating and updating values, we wanted to done with higher order function.

1条回答
家丑人穷心不美
2楼-- · 2020-05-02 09:13

Using a recursive method that performs the update

func update(key:String, in dict: [String:Any], with value: Any) -> [String:Any] {
    var out = [String:Any]()
    if let _ = dict[key] {
        out = dict
        out[key] = value
    } else {
        dict.forEach {
            if let innerDict = $0.value as? [String:Any] {
                out[$0.key] = update(key: key, in: innerDict, with: value)
            } else {
                out[$0.key] = $0.value
            }
        }
    }
     return out
}

we can use a simple map call

var original = [["currentObject": ["passport": 0, "pan_card": 0, "ration_card": 0], "title": "Documents list"], ["currentObject": ["pan_card": 0, "dl": 0, "voter": 0], "title": "Second Documents list"]]
let result = original.map{ update(key: "pan_card", in: $0, with: 1)}

The update function was based on this answer

查看更多
登录 后发表回答