共计 574 个字符,预计需要花费 2 分钟才能阅读完成。
案例如下
let list = [
{
defineCode: "001",
defineName: "分级名称 1",
gradeType: "分级类型 1",
orgVos: [
{
orgCode: "组织编码 1",
orgName: "组织名称 1",
},
],
},
{
defineCode: "001",
defineName: "分级名称 1",
gradeType: "分级类型 1",
orgVos: [
{
orgCode: "组织编码 2",
orgName: "组织名称 2",
},
],
},
{
defineCode: "002",
defineName: "分级名称 2",
gradeType: "分级类型 2",
orgVos: [
{
orgCode: "组织编码 2",
orgName: "组织名称 2",
},
],
},
];
const mergedList = list.reduce((acc, curr) => {const existingItem = acc.find(item => item.defineName === curr.defineName);
if (existingItem) {existingItem.orgVos = [...existingItem.orgVos, ...curr.orgVos];
} else {acc.push(curr);
}
return acc;
}, []);
console.log(mergedList)
打印内容如下
正文完
发表至: javascript
2024-03-01