Im trying to create a collection like this in order to use in a react component:
let data = [
{
group : 'A',
children : [
{ name : 'Animals', id : 22 },
...
]
},
{
group : 'B', children : [
{ name : 'Batteries', id : 7},
{ name : 'Baggage', id : 12 },
...
]
},
{
group : 'C', children : [
{ name : 'Cake', id : 7},
...
]
},
]
I've already sort my data like this :
let rawData = [
{ name : 'Animals', id : 10},
{ name : 'Batteries', id : 7},
{ name : 'Baggage', id : 12 },
{ name : 'Cake', id : 7},
...
]
Also I used this sorting method but the problem is, it's returning an Object with A
, B
, C
keys with children as values. But I have to turn it into array like above in order to use that.
Here is what i've tried so far :
let data = rawData.reduce(function(prevVal, newVal){
char = newVal.name[0].toUpperCase();
return { group: char, children :{name : newVal.name, id : newVal.id}};
},[])