Create array of objects based on another array?
- by xckpd7
I want to take an array like this:
var food = [
{
name: 'strawberry',
type: 'fruit',
color: 'red',
id: 3483
},
{
name: 'apple',
type: 'fruit',
color: 'red',
id: 3418
},
{
name: 'banana',
type: 'fruit',
color: 'yellow',
id: 3458
},
{
name: 'brocolli',
type: 'vegetable',
color: 'green',
id: 1458
},
{
name: 'steak',
type: 'meat',
color: 'brown',
id: 2458
},
]
And I want to create something like this dynamically:
var foodCategories = [
{
name: 'fruit',
items: [
{
name: 'apple',
type: 'fruit',
color: 'red',
id: 3418
},
{
name: 'banana',
type: 'fruit',
color: 'yellow',
id: 3458
}
]
},
{
name: 'vegetable',
items: [
{
name: 'brocolli',
type: 'vegetable',
color: 'green',
id: 1458
},
]
},
{
name: 'meat',
items: [
{
name: 'steak',
type: 'meat',
color: 'brown',
id: 2458
}
]
}
]
What's the best way to go about doing this?