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

基于_DocumentNumber合并多维动态JSON数组的需求

合并多维JSON数组中_DocumentNumber相同的元素

原始JSON示例

{
    "TEST": [{
        "_DocumentType": "INVOICE",
        "_DocumentDate": "2022-12-31",
        "_AccountNumber": "910:11:77:44520",
        "_DocumentNumber": "BILL-3270",
        "Detail": [{
                "test": "INVOICE",
                "test2": "2022-10-21"
            },
            {
                "test": "INVOICE",
                "test2": "2022-10-21"
            }
        ]
    }, {
        "_DocumentType": "INVOICE",
        "_DocumentDate": "2022-12-31",
        "_AccountNumber": "910:11:77:44520",
        "_DocumentNumber": "BILL-2543",
        "Detail": [{
                "test": "INVOICE",
                "test2": "2022-12-31"
            },
            {
                "test": "INVOICE",
                "test2": "2022-12-31"
            }
        ]
    }, {
        "_DocumentType": "INVOICE",
        "_DocumentDate": "2022-12-31",
        "_AccountNumber": "910:11:77:44520",
        "_DocumentNumber": "BILL-3270",
        "Detail": [{
                "test": "INVOICE",
                "test2": "2022-12-31"
            },
            {
                "test": "INVOICE",
                "test2": "2022-12-31"
            }
        ]
    }]
}

需求

将TEST数组中_DocumentNumber字段值相同的元素合并,合并时把这些元素的Detail数组合并为一个数组;_DocumentNumber不同的元素保持独立。

解决方案(JavaScript实现)

用Array.reduce()方法分组处理即可,代码如下:

const originalData = { /* 填入你的原始JSON数据 */ };

// 按_DocumentNumber分组并合并Detail数组
const mergedGroups = originalData.TEST.reduce((acc, item) => {
    const key = item._DocumentNumber;
    if (!acc[key]) {
        // 首次遇到该编号,直接存入分组
        acc[key] = { ...item, Detail: [...item.Detail] };
    } else {
        // 已有该编号,追加Detail数组内容
        acc[key].Detail.push(...item.Detail);
    }
    return acc;
}, {});

// 将分组对象转换为数组,得到最终结果
const mergedData = {
    TEST: Object.values(mergedGroups)
};

console.log(JSON.stringify(mergedData, null, 2));

合并后的结果

{
  "TEST": [
    {
      "_DocumentType": "INVOICE",
      "_DocumentDate": "2022-12-31",
      "_AccountNumber": "910:11:77:44520",
      "_DocumentNumber": "BILL-3270",
      "Detail": [
        {
          "test": "INVOICE",
          "test2": "2022-10-21"
        },
        {
          "test": "INVOICE",
          "test2": "2022-10-21"
        },
        {
          "test": "INVOICE",
          "test2": "2022-12-31"
        },
        {
          "test": "INVOICE",
          "test2": "2022-12-31"
        }
      ]
    },
    {
      "_DocumentType": "INVOICE",
      "_DocumentDate": "2022-12-31",
      "_AccountNumber": "910:11:77:44520",
      "_DocumentNumber": "BILL-2543",
      "Detail": [
        {
          "test": "INVOICE",
          "test2": "2022-12-31"
        },
        {
          "test": "INVOICE",
          "test2": "2022-12-31"
        }
      ]
    }
  ]
}

补充说明

  • 代码默认同一_DocumentNumber对应的其他字段(如_DocumentType、_DocumentDate)值完全一致,若存在不一致的情况,可根据需求调整逻辑(比如保留第一个元素的字段值,或额外处理差异字段)。
  • 使用扩展运算符...是为了避免修改原始数据的引用,保证数据独立性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 21:44:56