[英]How to add an property to an existing object inside an array
For example this is the original array例如这是原始数组
[
{name:xyz,id:123 },
{name:abc,id:232},
]
Now here is an another array现在这是另一个数组
[
{value:'anything'},
{value:'anything12312'}
]
Now in new array or original array the output should be this现在在新数组或原始数组中 output 应该是这个
[
{value:'anything',name:xyz,id:123},
{value:'anything12312', name:abc,id:232}}
]
How can I achieve this我怎样才能做到这一点
Use object destructuring.使用 object 解构。
const arr1 = [
{name:'xyz',id:123 },
{name:'abc',id:232},
]
const arr2 = [
{value:'anyrhing'},
{value:'anything12312'}
]
const arr3 = [];
for(let i=0;i<arr1.length;i++)
arr3.push({...arr2[i],...arr1[i]});
A1=[
{name:"xyz",id:123 },
{name:"abc",id:232},
];
A2=[
{value:'anyrhing'},
{value:'anything12312'}
];
obj1={
...A2[0],
...A1[0]}
//another method to merge objects
obj2=Object.assign(A2[1],A1[1]);
//The result you needed
A3=[obj1,obj2];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.