Input:
var animals = [
{category: "mammal", name: "dog", categoryID: "MAM"},
{category: "fish", name: "pickerel", categoryID: "FIS"},
{category: "mammal", name: "cat", categoryID: "MAM"},
{category: "mammal", name: "monkey", categoryID: "MAM"},
{category: "bird", name: "budgie", categoryID: "BIRD"}
];
Desired output:
var categories = [
{name: "mammal", catId: "MAM"},
{name: "bird", catId: "BIR"},
{name: "fish", catId: "FIS"}
];
Is there a nice way to do it with underscore?
The closest I got was this jsFiddle:
var categories = _.groupBy(animals, function(animal) {
return animal.category;
});
With this output:
Object {mammal: Array[3], fish: Array[1], bird: Array[1]}
But is there a way to return the desired output above in a nice way?