[英]how to add object in nested array of objects without mutating original source
I have multi nested array of objects, in which I need to merge objects in the grandChildren based on the id without mutation我有多个嵌套的对象数组,其中我需要根据没有突变的 id 合并 grandChildren 中的对象
Details ......细节 ......
###Example ###例子
let arr1 = {
"initiateLevel": true,
"parent": [
{
"name": "level1",
"childrens": [
{
"group": "Level-group",
"grandChildrens": [
{
"id": 21,
"technology": "sp1",
"path": "l2"
},
{
"id": 22,
"technology": "sp2",
"path": "l2"
}
]
}
]
},
{
"name": "level2",
"childrens": [
{
"group": "Level-group-2",
"grandChildrens": [
{
"id": 121,
"technology": "sp12",
"path": "l4"
},
{
"id": 122,
"technology": "sp22",
"path": "l4"
}
]
}
]
}
]
}
ex: Below object needs to merged with the array based on the id例如:下面的对象需要与基于 id 的数组合并
let newobj=
[
{
"id": 22,
"reason": "reason 2",
"phase": "phase 2",
"reviewer": "by user 2",
"date": "date 2"
},
{
"id": 21,
"reason": "reason 1",
"phase": "phase 1",
"reviewer": "by user 1",
"date": "date 1"
}
]
expected output:
预期输出:
{
"initiateLevel": true,
"parent": [
{
"name": "level1",
"childrens": [
{
"group": "Level-group",
"grandChildrens": [
{
"id": 21,
"technology": "sp1",
"path": "l2",
"reason": "reason 1",
"phase": "phase 1",
"reviewer": "by user 1",
"date": "date 1"
},
{
"id": 22,
"technology": "sp2",
"path": "l2",
"reason": "reason 2",
"phase": "phase 2",
"reviewer": "by user 2",
"date": "date 2"
}
]
}
]
},
{
"name": "level2",
"childrens": [
{
"group": "Level-group-2",
"grandChildrens": [
{
"id": 121,
"technology": "sp12",
"path": "l4"
},
{
"id": 122,
"technology": "sp22",
"path": "l4"
}
]
}
]
}
]
}
I tried to like this.我试着喜欢这个。 but it's not working
但它不工作
const merge = (y, z) => {
y.parent.forEach((element) => {
element.childrens.forEach((x) => {
x.grandChildrens.forEach((test) => {
const reviewIndex = z.findIndex(
(reviewItem) => reviewItem.id === test.id
);
if(reviewIndex>=0)
{
return {...test, ...z[reviewIndex]}
}
});
});
});
};
merge(arr1,newobj)
How to merge the object based on the id without mutation.如何根据 id 合并对象而不进行变异。
Use Array.prototype.map
instead, forEach
gets no returns改用
Array.prototype.map
, forEach
没有回报
let newobj = [ { id: 22, reason: 'reason 2', phase: 'phase 2', reviewer: 'by user 2', date: 'date 2' }, { id: 21, reason: 'reason 1', phase: 'phase 1', reviewer: 'by user 1', date: 'date 1' } ]; let arr1 = { initiateLevel: true, parent: [ { name: 'level1', childrens: [ { group: 'Level-group', grandChildrens: [ { id: 21, technology: 'sp1', path: 'l2', reason: 'reason 1', phase: 'phase 1', reviewer: 'by user 1', date: 'date 1' }, { id: 22, technology: 'sp2', path: 'l2', reason: 'reason 2', phase: 'phase 2', reviewer: 'by user 2', date: 'date 2' } ] } ] }, { name: 'level2', childrens: [ { group: 'Level-group-2', grandChildrens: [ { id: 121, technology: 'sp12', path: 'l4' }, { id: 122, technology: 'sp22', path: 'l4' } ] } ] } ] }; const merge = (y, z) => { const parent = y.parent.map((element) => { return { ...element, childrens: element.childrens.map((x) => { return { ...x, grandChildrens: x.grandChildrens.map((test) => { return { ...test, ...z.find((reviewItem) => reviewItem.id === test.id) }; }) }; }) } }); return { ...y, parent }; }; const arr2 = merge(arr1, newobj); console.log(arr2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.