INPUT
[0]: [{}, {}],
[1]: [{}, {}],
[2]: [{}]
I have index 0, 1 and 2 with array of objects. Now I need to merge all these index values into a single array of object
Expected O/P
[{}, {}, {}, {}, {}]
INPUT
[0]: [{}, {}],
[1]: [{}, {}],
[2]: [{}]
I have index 0, 1 and 2 with array of objects. Now I need to merge all these index values into a single array of object
Expected O/P
[{}, {}, {}, {}, {}]
let arr = [
[{
key1: "value1"
}, {
key2: "value2"
}],
[{
key3: "value3"
}, {
key4: "value4"
}],
[{
key5: "value5"
}]
];
arr = arr.flat();
console.log(arr);
Here's just one solution like you need:
let a = {
0: [{}, {}],
1: [{}, {}],
2: [{}]
}
let values = []
for (var x in a)
{
a[x].map(p => values.push(p))
}
console.log(values) //[{}, {}, {}, {}, {}]
Make sure to handle each case, validate if it's an array then loop over it.
You can do a one liner using the reduce
:
const obj = {
0: [{}, {}],
1: [{}, {}],
2: [{}]
}
const arr = Object.values(obj).reduce((acc, curr) => ([...acc, ...curr]), [])
console.log(arr)