For a json object like this,
list=[
{name:"hello",
category:"verb"},
{name:"world",
category:"noun"}
];
What would be the fastest way to categorize the array using underscore to get this:
category=[
{id:"verb",
list:[
{name:"hello",
category:"verb"}
]},
{id:"noun",
list:[
{name:"world",
category:"noun"}
]}
];
It should be some kind of chained map-reduce… Of course I could do this easily using _.filter (but that would be slow), or using a for loop.
OK, I found it:
groupBy _.groupBy(list, iterator)
Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of a function, groups by the property named by iterator on each of the values.
So I did it using this(I already had a list):