我有一个像这样的字典数组......
[
{
"id" : "3",
"sellingPrice" : "520",
"quantity" : "15"
},
{
"id" : "5",
"sellingPrice" : "499",
"quantity" : "-1"
},
{
"id" : "8",
"sellingPrice" : "500",
"quantity" : "79"
}
]
现在我想在字典中添加另一个名为remaining_balance
的密钥,其值为420,499&分别为500。我怎样才能做到这一点..?希望有人能帮忙......
答案 0 :(得分:1)
假设您有一系列字典,如下所示:
var arrayOfDictionaries = [
[
"id": 3,
"sellingPrice": 520,
"quantity": 15
]
]
arrayOfDictionaries
不是let
常量非常重要,否则会被视为不可变,您无法在其上调用append
。
现在你创建一个新的字典,如:
let newDictionary = [
"id": 10,
"remaining_balance": 420,
"quantity": 15
]
现在添加newDictionary
之类的
arrayOfDictionaries.append(newDictionary)
如果订单很重要,有几种方法可以解决这个问题。
调用append
时,新值(在这种情况下是新词典)将始终插入数组的底部。
如果由于某种原因您无法以正确的顺序拨打append
,可以使用insert
将字典插入特定位置。
另一种方法是疯狂追加值,完成后,在数组上调用sort
。
请注意,对于值,我没有使用字符串,因为您只有"id" : 30
之类的数字。
此外,如果您希望调用第二个密钥remaining_balance
,则应拨打第一个密钥selling_price
而不是sellingPrice
。因为一致性。
据我了解,您正在尝试实施一些负责销售某些产品的软件。
我认为你是从一个完全错误的方面解决这个问题。 我想你应该阅读有关数据库关系的内容。实际销售产品是一个非常普遍的问题。
也许this会帮助你。我会自己提供一个可能的解决方案,但我认为这忽略了你的问题。
如果您决定使用数据库方法,则不一定必须使用数据库。您可以采用该方法并使用简单的结构/类/数组实现它。
答案 1 :(得分:1)
好像你想在你的字典中添加一个数组值:
var arrDict = Array<Dictionary<String,Any>>() //Your array
arrDict.append(["id":"3","sellingPrice":"520","quantity":"13"])
arrDict.append(["id":"5","sellingPrice":"43","quantity":"32"])
arrDict.append(["id":"8","sellingPrice":"43","quantity":"33"])
let arrValue = ["420","499","500"] //Your remaining value in array
print("Before ",arrDict)
for (index,dict) in arrDict.enumerated() {
var dictCopy = dict //assign to var variable
dictCopy["remaining_balance"] = arrValue[index]
arrDict[index] = dictCopy //Replace at index with new dictionary
}
print("After ",arrDict)
修改强>
如果你能保留一个数组的索引就可以了, 假设你有一个数组的索引
var dictCopy = arrDict[index]
dictCopy["remaining_balance"] = "666" //Your calculated value
arrDict[index] = dictCopy //Replace at index with new dictionary
答案 2 :(得分:0)
var newKV = [["remaining_balance": "420"],["remaining_balance": "490"],["remaining_balance": "500"]]
let array = [["id":"3", "sellingPrice":"520", "quantity":"15"], ["id":"5", "sellingPrice":"520", "quantity":"15"], ["id":"8", "sellingPrice":"520", "quantity":"15"]]
let newArray = array.enumerated().map { (index : Int, value: [String: String]) -> [String: String] in
var dic = value
dic.merge(newKV[index]) { (_, new) -> String in
new
}
return dic
}
答案 3 :(得分:0)
您可以通过映射您的数组实现它:
var myArray = [["id": "3", "sellingPrice": "520", "quantity" : "15"], ["id": "5", "sellingPrice": "499", "quantity" : "-1"], ["id": "8", "sellingPrice": "500", "quantity" : "79"]]
print(myArray)
/*
[["id": "3", "sellingPrice": "520", "quantity": "15"],
["id": "5", "sellingPrice": "499", "quantity": "-1"],
["id": "8", "sellingPrice": "500", "quantity": "79"]]
*/
print("___________________")
var remainingBalanceDesriedValue = 420
myArray = myArray.map { (dict: [String: String]) -> [String: String] in
var copyDict = dict
copyDict["remaining_balance"] = "\(remainingBalanceDesriedValue)"
remainingBalanceDesriedValue = (remainingBalanceDesriedValue == 420) ? 499 : (remainingBalanceDesriedValue == 499) ? 500 : 420
return copyDict
}
print(myArray)
/*
[["sellingPrice": "520", "quantity": "15", "id": "3", "remaining_balance": "420"],
["sellingPrice": "499", "quantity": "-1", "id": "5", "remaining_balance": "499"],
["sellingPrice": "500", "quantity": "79", "id": "8", "remaining_balance": "500"]]
*/
答案 4 :(得分:0)
我注意到这个问题缺少extension
答案,是的。我会成为那个人,所以现在就是这样。通过支持其他类型的词典,可以使这更加通用,随意投入;)
来自@ eason的答案的灵感。
var newKV = [["remaining_balance": "420"],["remaining_balance": "490"],["remaining_balance": "500"]]
var array = [["id":"3", "sellingPrice":"520", "quantity":"15"], ["id":"5", "sellingPrice":"520", "quantity":"15"], ["id":"8", "sellingPrice":"520", "quantity":"15"]]
extension Array where Element == [String: String] {
enum UniquingKeysStrategy {
case old
case new
}
mutating func merge(with target: Array<Element>, uniquingKeysWith: UniquingKeysStrategy = .new) {
self = self.merged(with: target)
}
func merged(with target: Array<Element>, uniquingKeysWith strategy: UniquingKeysStrategy = .new) -> Array<Element> {
let base = self.count > target.count ? self : target
let data = self.count > target.count ? target : self
return data.enumerated().reduce(into: base, {
result, data in
result[data.offset]
.merge(data.element, uniquingKeysWith: {
old, new in
if strategy == .new { return new }
return old
})
})
}
}
let mergedArrays = newKV.merged(with: array, uniquingKeysWith: .old)
array.merge(with: newKV)
快乐编码:)