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

Node.js中按subject键合并嵌套JSON数组的实现需求

在Node.js中按Subject合并嵌套JSON数组的简洁实现

嘿,我完全懂你遇到的问题——按公共键合并嵌套JSON数组,同时给缺失的条目填充null,如果想复杂了确实头疼。这里有个简洁且可扩展的实现方案,支持任意数量的子数组:

原始输入数据

[ 
  [ 
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R02-03" }, "subject": "001-BB" } }, 
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R02-03" }, "subject": "002-CC" } }, 
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R01-02" }, "subject": "010-A1" } }, 
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R33-33" }, "subject": "011-AB" } } 
  ], 
  [ 
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "001-BB" } }, 
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "002-CC" } }, 
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032333" }, "subject": "99-001" } }, 
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032111" }, "subject": "test-111" } } 
  ] 
]

期望输出结果

[ 
  [ 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": "R02-03" }, "value1": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "001-BB" } }, 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": "R02-03" }, "value1": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "002-CC" } }, 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": "R01-02" }, "value1": { "item_type": "INT", "Serial number of device given to the subject": null }, "subject": "010-A1" } }, 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": "R33-33" }, "value1": { "item_type": "INT", "Serial number of device given to the subject": null }, "subject": "011-AB" } }, 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": null }, "value1": { "item_type": "INT", "Serial number of device given to the subject": "13032333" }, "subject": "99-001" } }, 
    { "f_measure_function": { "value0": { "item_type": "ST", "Randomization number": null }, "value1": { "item_type": "INT", "Serial number of device given to the subject": "13032111" }, "subject": "test-111" } } 
  ] 
]

简洁实现方案

核心思路是用一个对象按subject做分组存储,遍历每个子数组时把对应的值填充到分组条目里,最后把对象转成数组即可。这个方案支持任意数量的子数组,自动生成对应的value0、value1...valueN字段:

// 你的原始嵌套数组
const nestedArray = [
  [
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R02-03" }, "subject": "001-BB" } },
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R02-03" }, "subject": "002-CC" } },
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R01-02" }, "subject": "010-A1" } },
    { "f_measure_function": { "value": { "item_type": "ST", "Randomization number": "R33-33" }, "subject": "011-AB" } }
  ],
  [
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "001-BB" } },
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032355" }, "subject": "002-CC" } },
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032333" }, "subject": "99-001" } },
    { "f_measure_function": { "value": { "item_type": "INT", "Serial number of device given to the subject": "13032111" }, "subject": "test-111" } }
  ]
];

// 用对象按subject分组存储合并后的条目
const subjectMap = {};

// 遍历每个子数组,同时记录当前子数组的索引(用来生成value0、value1...)
nestedArray.forEach((subArray, subIndex) => {
  subArray.forEach(item => {
    const { f_measure_function } = item;
    const { subject, value } = f_measure_function;

    // 如果当前subject还未被初始化,先创建基础结构并给所有value字段设为null
    if (!subjectMap[subject]) {
      subjectMap[subject] = {
        f_measure_function: {
          subject,
          // 根据子数组数量生成对应的valueX字段,默认null
          ...nestedArray.reduce((acc, _, idx) => {
            acc[`value${idx}`] = null;
            return acc;
          }, {})
        }
      };
    }

    // 将当前子数组的value赋值到对应的valueX字段
    subjectMap[subject].f_measure_function[`value${subIndex}`] = { ...value };
  });
});

// 将map的值转为数组,外层再包一层数组匹配期望格式
const mergedArray = [Object.values(subjectMap)];

// 打印结果
console.log(JSON.stringify(mergedArray, null, 2));

方案说明

  1. 分组存储:用subjectMap对象把相同subject的条目聚合到一起,避免重复遍历查找。
  2. 自动初始化:第一次遇到某个subject时,会根据子数组的数量自动创建所有valueX字段并设为null,确保格式统一。
  3. 可扩展性:不管主数组里有2个还是更多子数组,代码都能自动适配,生成对应的value字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:36:58