You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DataWeave中对扁平对象数组执行groupBy操作实现数据合并

实现方案

核心思路

  • 首先确定分组维度为no、sendingStoreCode、receivingStoreCode三个字段,将三个字段拼接为唯一的分组键
  • 用临时映射结构存储每个分组的公共信息和子项列表
  • 遍历原始数据,将对应字段归入分组的子项列表,同时自动生成子项的line行号(按加入顺序自增)
  • 遍历完成后将临时映射的值转为数组即可得到目标结构

注意:如果业务中三个分组字段可能包含拼接用的分隔符,可以更换为业务中不存在的特殊字符作为分隔符,Python环境下也可以直接用三个字段组成的元组作为分组键,避免冲突。

代码实现

JavaScript 版本

function groupDeliveryData(rawData) {
  const groupMap = {}
  rawData.forEach(item => {
    // 生成唯一分组键
    const groupKey = `${item.no}|${item.sendingStoreCode}|${item.receivingStoreCode}`
    // 分组不存在则初始化
    if (!groupMap[groupKey]) {
      groupMap[groupKey] = {
        no: item.no,
        sendingStoreCode: item.sendingStoreCode,
        receivingStoreCode: item.receivingStoreCode,
        deliveryItems: []
      }
    }
    // 加入子项,line为当前子项长度+1
    groupMap[groupKey].deliveryItems.push({
      line: groupMap[groupKey].deliveryItems.length + 1,
      sku: item.sku,
      quantity: item.quantity
    })
  })
  // 转成数组返回
  return Object.values(groupMap)
}

// 调用示例
const raw = [{"no":"2001","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"dddd","quantity":1},{"no":"2001","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"xxxx","quantity":1},{"no":"2002","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"xxxx","quantity":1}]
console.log(groupDeliveryData(raw))

Python 版本

def group_delivery_data(raw_data):
    group_map = {}
    for item in raw_data:
        # 生成唯一分组键
        group_key = f"{item['no']}|{item['sendingStoreCode']}|{item['receivingStoreCode']}"
        # 分组不存在则初始化
        if group_key not in group_map:
            group_map[group_key] = {
                "no": item["no"],
                "sendingStoreCode": item["sendingStoreCode"],
                "receivingStoreCode": item["receivingStoreCode"],
                "deliveryItems": []
            }
        # 加入子项,line为当前子项长度+1
        group_map[group_key]["deliveryItems"].append({
            "line": len(group_map[group_key]["deliveryItems"]) + 1,
            "sku": item["sku"],
            "quantity": item["quantity"]
        })
    # 转成列表返回
    return list(group_map.values())

# 调用示例
raw = [{"no":"2001","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"dddd","quantity":1},{"no":"2001","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"xxxx","quantity":1},{"no":"2002","sendingStoreCode":"006091","receivingStoreCode":"006095","sku":"xxxx","quantity":1}]
print(group_delivery_data(raw))

内容的提问来源于stack exchange,提问作者maomifadacai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 18:36:05