gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:30:20 24 4
gpt4 key购买 nike

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

我们需要更新 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 中的每个键

也尝试了这个,但是它添加了新的 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)

我需要如下输出

原始值

[["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

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

最佳答案

使用递归方法执行更新

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

更新函数基于this answer

关于ios - 字典数组中特定键的映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56814962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com