Using underscore groupby to group an array of cars by their colour

Viewed 69486

I have an array of cars.

car = {
    make: "nissan",
    model: "sunny",
    colour: "red"
};

How would I use underscore.js to group the array by colour?

I've tried a few combos but I'm not really sure how to specify my iterator condition:

var carsGroupedByColor = _.groupBy(cars, false, colour);
var carsGroupedByColor = _.groupBy(vars, false, function(cars){ return cars[colour]; };

They all return everything in the array each time.

3 Answers
Related