首页 > 解决方案 > 映射字典数组中特定键的值

问题描述

映射字典数组中出现的特定键的值并将其替换在同一数组中。

我们需要更新pan_card0 到 1,出现字典数组。

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)

此代码更新currentObject中的每个键

并尝试了这个,但它添加了新的密钥

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)

我需要如下输出

原值

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

更新后

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

参考文档 链接

我们知道手动迭代和更新值,我们想用高阶函数来完成。

标签: iosiphonehigher-order-functionsswift5

解决方案


使用执行更新的递归方法

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
}

我们可以使用一个简单的map调用

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)}

update功能基于此答案


推荐阅读