数组处理,去重&合并相同key的值

背景:

上游返回的 skuIdList 存在相同的Id,skuCount数组与skuID一一对应

处理结果:skuIdList 去重,对应的count总和要加起来,对应的 originalPriceList 和 subtotalPriceList 不变
代码:

import groovy.json.JsonSlurper
def skuIdList = new JsonSlurper().parseText('["472262851","472302672","472284575","472262851"]');
//logs.add('skuIdList:\n' + skuIdList.toString());
def applySkuList = [];
def originalPriceList = new JsonSlurper().parseText("[0.03,510.0,51.0,0.03]");
def subtotalPriceList = new JsonSlurper().parseText("[0.21,3570.0,408.0,0.21]");
def skuCountList = new JsonSlurper().parseText("[7,7,8,7]");
def parentPackageId = 5008466108;
 
def skuIdCountMap = [:];
def originalPriceList2 =[];
def subtotalPriceList2 =[];
//通过map去重skuID,并重新写对应价格的数组,使其一一对应
for (int i = 0; i < skuIdList.size(); i++) {
    def currentSkuId = skuIdList[i];
    def currentCount = skuCountList[i];
    if(skuIdCountMap.containsKey(currentSkuId)){
        skuIdCountMap[currentSkuId] += currentCount;
    } else {
        skuIdCountMap[currentSkuId] = currentCount; 
        originalPriceList2.add(originalPriceList[i]);
        subtotalPriceList2.add(subtotalPriceList[i]);
    }
}
/*
遍历skuIdCountMap
分别把skuID 与 对应count 写入2个数组
*/
def skuIDs = [];
def skuCounts = [];
for (String key : skuIdCountMap.keySet()) {
   skuCounts.add(skuIdCountMap[key])
    skuIDs.add(key);
}
println skuIdCountMap;
println originalPriceList2;
println subtotalPriceList2;
println skuIDs;
println skuCounts;

运行结果

[472262851:14, 472302672:7, 472284575:8]
[0.03, 510.0, 51.0]
[0.21, 3570.0, 408.0]
[472262851, 472302672, 472284575]
[14, 7, 8]

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于一个树形数组,可以使用递归的方式将其扁平化。具体实现可以参考以下代码: ```javascript function flatten(arr, result = []) { for (let i = 0; i < arr.length; i++) { const item = arr[i]; result.push(item); if (Array.isArray(item.children)) { flatten(item.children, result); } } return result; } ``` 上述代码会将树形数组扁平化为一维数组。然后,我们可以使用数组的 `reduce` 方法对其进行合并去重。具体实现可以参考以下代码: ```javascript function merge(arr) { const map = new Map(); return arr.reduce((result, item) => { const key = item.id; if (!map.has(key)) { map.set(key, item); result.push(item); } else { const existItem = map.get(key); existItem.count += item.count; } return result; }, []); } ``` 上述代码会将相同 `id` 的项进行合并,并将合并后的结果保存在一个新数组中返回。 最终,我们可以将以上两个函数组合起来使用,如下所示: ```javascript const tree = [ { id: 1, count: 1, children: [ { id: 2, count: 2, children: [ { id: 3, count: 3, children: [ { id: 4, count: 4, }, ], }, ], }, ], }, { id: 1, count: 2, children: [ { id: 2, count: 3, children: [ { id: 3, count: 4, children: [ { id: 5, count: 5, }, ], }, ], }, ], }, ]; const flat = flatten(tree); const merged = merge(flat); console.log(merged); // Output: // [ // { id: 1, count: 3 }, // { id: 2, count: 5 }, // { id: 3, count: 7 }, // { id: 4, count: 4 }, // { id: 5, count: 5 } // ] ``` 上述代码会将树形数组扁平化、合并去重,得到最终的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值