Reputation: 101
I have an object with key values, and those keys each contain an array of objects:
var obj = {
"0": [
{
"category": "A",
"index": 0,
"property": "Name",
"value": "Bob"
},
{
"category": "A",
"index": 0,
"property": "Surname",
"value": "Dylan"
}
],
"1": [
{
"category": "A",
"index": 1,
"property": "Name",
"value": "Elvis"
},
{
"category": "A",
"index": 1,
"property": "Surname",
"value": "Presley"
}
]
}
How would I go about merging the objects into one single array with the objects combined therein? The objective is to have the result return the following:
var obj2 = [
{
"category": "A",
"index": 0,
"property": "Name",
"value": "Bob"
},
{
"category": "A",
"index": 0,
"property": "Surname",
"value": "Dylan"
},
{
"category": "A",
"index": 1,
"property": "Name",
"value": "Elvis"
},
{
"category": "A",
"index": 1,
"property": "Surname",
"value": "Presley"
}
]
I've tried to make use of LoDash union and join, however to no avail.
Upvotes: 0
Views: 880
Reputation: 64725
Assuming your example was wrong, and it's actually like below (since what you put in isn't even valid), you can just use Object.values(obj).flat()
var obj = {
"0": [{
"category": "A",
"index": 0,
"property": "Name",
"value": "Bob"
}],
"1": [{
"category": "A",
"index": 1,
"property": "Name",
"value": "Jessica"
}]
}
console.log(Object.values(obj).flat())
Upvotes: 1