映射值,尤其是字典数组中的键 [英] Map value in particular key in array of dictionary

查看:83
本文介绍了映射值,尤其是字典数组中的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在出现字典数组时,我们需要将 pan_card 键从 0更新为1 .

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)

此代码更新 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)

我需要如下输出

原始值

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

参考文档链接

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

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

推荐答案

使用执行更新的递归方法

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
}

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

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

update函数基于此答案

这篇关于映射值,尤其是字典数组中的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆