字典数组中特定键的映射值

时间:2019-06-29 03:55:45

标签: ios iphone higher-order-functions swift5

将值指定为字典数组中出现的特定键,并将其替换为同一数组。

在出现字典数组时,我们需要将 pan_card 键从 0更新为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"]] 

引荐文件Link

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

1 个答案:

答案 0 :(得分:1)

使用执行更新的递归方法

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函数基于this answer