如果一个键在数组中重复,我想添加该键的值 [英] If a key is duplicated in an array,I want to add the values of that key

查看:35
本文介绍了如果一个键在数组中重复,我想添加该键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

var 数组 = [[产品":Item0",价格":15"],[产品":项目 1",价格":53"],[产品":Item2",价格":12"],[产品":项目 1",价格":83"],[产品":Item0",价格":10"],[产品":Item3",价格":88"],[产品":Item0",价格":44"]]

我想像这样改变这个数组.

<预><代码>[[产品":item0",价格":69"],[产品":item1",价格":136"],[产品":item2",价格":12"],[产品":item3",价格":88"]]

我该怎么办?

这是我写的代码.但是,如果重复了两个以上的键,则不会显示确切的值.你能在这里修复一下还是给我一个新的方法?

var 数组= [[产品":Item0",价格":15"],[产品":项目 1",价格":53"],[产品":Item2",价格":12"],[产品":项目 1",价格":83"],[产品":Item0",价格":10"],[产品":Item3",价格":88"],[产品":Item0",价格":44"]]varfilteredArrays = [[String:String]]()变量总和:整数 = 0对于我在 0..<arrayOfDicts.count {让 Product1 = arrayOfDicts[i]["Product"]如果(我== 0){filteredArrays.append(arrayOfDicts[i])} 别的 {var 标志 = 假对于 j in 0..

谢谢

解决方案

  1. 迭代您的产品
  2. 检查产品"是否包含有效字符串
  3. 将价格提取为 Int(或根据需要浮动).默认为零以避免错误的总和.
  4. 创建一个以产品名称为键的字典,并对具有相同键的值求和
  5. 将字典转换为数组

    var 结果 = [String : Int]()对于数组中的产品{如果让 productKey = product["Product"] {让值 = Int(product["Price"] ?? "0")如果 result[productKey] == nil,让 value = value {结果[productKey] = 值} else if let value = value {结果[产品密钥]!+= 值}}}let newArray = result.map {["Product":$0, "Price": $1]}

If a key is duplicated in an array, I want to combine the values of that key. For example, I have the following array.

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

And I want to change this array like this.

[
    ["Product": "item0", "Price": "69"],
    ["Product": "item1", "Price": "136"],
    ["Product": "item2", "Price": "12"],
    ["Product": "item3", "Price": "88"]
]

What should I do?

This is the code I wrote. However, if more than two keys are duplicated, the exact value is not displayed. Could you fix a little here or give me a new way at all?

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

}

Thank you

解决方案

  1. Iterate through your products
  2. Check if "Product" contains valid string
  3. Extract price as Int (or float if needed). Default as zero to avoid wrong sum.
  4. Create a dictionary with product name as key and sum the values with same key
  5. Convert dictionary to array

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

这篇关于如果一个键在数组中重复,我想添加该键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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