Using underscore groupby to group an array of cars by their colour
问题 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. 回答1: You don't need the false second argument, the following will work: