簡體   English   中英

映射值,尤其是字典數組中的鍵

[英]Map value in particular key in array of dictionary

映射值將特定的鍵顯示在字典數組中的任何位置,並將其替換在同一數組中。

如果字典數組出現,我們需要將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"]] 

引用文檔鏈接

我們知道手動迭代和更新值,我們想使用高階函數來完成

使用執行更新的遞歸方法

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功能基於此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM