Swift 如果一个键在数组中重复,我想添加该键的值

Swift 如果一个键在数组中重复,我想添加该键的值,swift,nsarray,Swift,Nsarray,如果一个键在数组中重复,我希望组合该键的值。例如,我有以下数组 var arrays = [ ["Product":"Item0", "Price":"15"], ["Product":"Item1", "Price":"53"], ["Product":"Item2", "Price":"12"], ["Product":"Item1", "Price":"83"], ["Product":"Item0", "Price":"10"], ["Pr

如果一个键在数组中重复,我希望组合该键的值。例如,我有以下数组

var arrays = [
    ["Product":"Item0", "Price":"15"],
    ["Product":"Item1", "Price":"53"],
    ["Product":"Item2", "Price":"12"],
    ["Product":"Item1", "Price":"83"],
    ["Product":"Item0", "Price":"10"],
    ["Product":"Item3", "Price":"88"],
    ["Product":"Item0", "Price":"44"]
]
我想这样改变这个数组

[
    ["Product": "item0", "Price": "69"],
    ["Product": "item1", "Price": "136"],
    ["Product": "item2", "Price": "12"],
    ["Product": "item3", "Price": "88"]
]
我该怎么办

这是我写的代码。但是,如果复制了两个以上的键,则不会显示确切的值。你能帮我修一下吗,或者给我一个新的方法

var arrays= [
    ["Product":"Item0", "Price":"15"],
    ["Product":"Item1", "Price":"53"],
    ["Product":"Item2", "Price":"12"],
    ["Product":"Item1", "Price":"83"],
    ["Product":"Item0", "Price":"10"],
    ["Product":"Item3", "Price":"88"],
    ["Product":"Item0", "Price":"44"]
]

var filteredArrays = [[String:String]]()
var sum : Int = 0

for i in 0..<arrayOfDicts.count {

    let Product1 = arrayOfDicts[i]["Product"]

    if(i == 0){
        filteredArrays.append(arrayOfDicts[i])
    } else {

        var flag = false
        for j in 0..<filteredArrays.count {

            let Product2:String = filteredArrays[j]["Product"]!

            if Product1 == Product2 {

                sum += (Int(arrayOfDicts[i]["Price"]!)! + Int(arrayOfDicts[j]["Price"]!)!)

                filteredArrays[j] = ["Product":"\(arrayOfDicts[i]["Product"]!)", "Price":"\(sum)"]

                sum = 0
                flag = true
            }
        }

        if !flag {
            filteredArrays.append(arrayOfDicts[i])
        }
    }

}
var数组=[
[“产品”:“项目0”,“价格”:“15”],
[“产品”:“项目1”,“价格”:“53”],
[“产品”:“项目2”,“价格”:“12”],
[“产品”:“项目1”,“价格”:“83”],
[“产品”:“项目0”,“价格”:“10”],
[“产品”:“项目3”,“价格”:“88”],
[“产品”:“项目0”,“价格”:“44”]
]
var filteredarray=[[String:String]]()
变量和:Int=0
因为我在0。。
  • 反复浏览您的产品
  • 检查“产品”是否包含有效字符串
  • 将价格提取为整数(或浮动,如果需要)。默认为零以避免错误的和
  • 创建一个以产品名称为键的字典,并使用相同的键对值求和
  • 将字典转换为数组

    var result = [String : Int]()
    
    for product in arrays {
    
        if let productKey = product["Product"] {
             let value = Int(product["Price"] ?? "0")
             if result[productKey] == nil, let value = value {
                result[productKey] = value
             } else if let value = value {
                result[productKey]! += value
             }
        }
    }
    
    let newArray = result.map {["Product":$0, "Price": $1]}
    

  • 只是为了好玩,而不是胆小鬼,使用Swift 4新方法
    reduce(into:)
    和基于字典键的默认值下标:

    let result = arrays.reduce(into: [String : Int]()) {
        $0[$1["Product"] ?? "", default: 0] += Int($1["Price"] ?? "") ?? 0 }
        .map { ["Product": $0, "Price": $1] }
    


    “[[“产品”:“项目0”,“价格”:69],“产品”:“项目2”,“价格”: 12] ,[“产品”:“项目1”,“价格”:136],“产品”:“项目3”,“价格”: 88]]\n“

    print(result)