I'm working with the following data structure:
"data": {
"products": [
[
{
"category": "A",
"items": [
{
"name": "Aloe",
"price": 10
},
{
"name": "Apples",
"price": 5
}
]
},
{
"category": "B",
"items": [
{
"name": "Bread",
"price": 5
}
]
}
],
[
{
"category": "C",
"items": [
{
"name": "Candy",
"price": 5
},
{
"name": "Crayon",
"price": 5
}
]
},
{
"category": "D",
"items": [
{
"name": "Dice",
"price": 5
},
{
"name": "Doll",
"price": 10
}
]
}
]
]
}
I'd like extract parts of it to flatten so the results is as follows:
[
{
"name": "Aloe",
"price": 10
},
{
"name": "Apples",
"price": 5
},
{
"name": "Bread",
"price": 5
},
{
"name": "Candy",
"price": 5
},
{
"name": "Crayon",
"price": 5
},
{
"name": "Dice",
"price": 5
},
{
"name": "Doll",
"price": 10
}
]
How can I accomplish this?
I have tried this:
for (var sets in data.products) {
for (var categories in sets) {
for (var items in categories) {
for (var item in items) {
// assemble new array
}
}
}
}
... but had problems looping through the children objects. I've found a couple other similar questions, but they seem to address simpler data structures, and flatten the entire object rather than parts of it.
Any pointers would be appreciated.