Grouping by multiple fields in JavaScript or CoffeeScript using Underscore

Oct 19, 2012 Software Engineering 3,085 views
Grouping by multiple fields in JavaScript or CoffeeScript using Underscore

How to group by mupliple fields using Underscore for CoffeeScript and JavaScript:

# Group by field1 and field2
 groupedData = _.groupBy(data, (d) ->
   "#{d.field1}-#{d.field2}"
 )
groupby.coffee
// Group by field1 and field2
 var groupedData = _.groupBy(data, function(d) {
    return d.field1 + "-" + d.field2;
 });
groupby.js