I have a json file like this:
[
{
"stores": [
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
}
],
},
{
"stores": [
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
],
}
]
I want to combine the data so it is :
[
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
},
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
]
I started with a loop but I dont know how can I group all of them into 1:
const input = [
{
"stores": [
{
"name" : "My store",
"location" : "NY"
},
{
"name" : "Other store",
"location" : "FL"
},
{
"name" : "My other store",
"location" : "NY"
}
],
},
{
"stores": [
{
"name" : "Another My store",
"location" : "NY"
},
{
"name" : "Some Other store",
"location" : "FL"
},
{
"name" : "Secondary store",
"location" : "NY"
}
],
}
];
let grouped = []
input.forEach(item => {
const store = Object.keys(item.stores)
console.log(store)
})
2
Answers
Use
Array#flatMap()
:Try it: