I would like to know how to merge two array of objects into single array using javascript
var arr1 = [
{id:1, name: "xxx"}
]
var arr2 =[
{details:"finance", cost:"100"}
]
Tried.
var result1 = [...arr1, ...arr2];
var result=Object.assign({}, ...result1)
Expected Output
[
{
id:1,
name: "xxx",
details:"finance",
cost:"100"
}
]
3
Answers
Just inner-wrap you
[...arr1, ...arr2]
in curly braces and access via array index:[{...arr1[0], ...arr2[0]}]
You can also concat both arrays and then reduce the whole array to assign to a new object:
I prefer this because you can add as many arrays as you want to the concat function.