Javascript Array groupBy(accumulator)

Description

Javascript Array groupBy(accumulator)

Array.prototype.$groupBy = function(accumulator) {
  var result = {};

  this.forEach(function(n) {
    var key = accumulator(n);
    if (result[key] === undefined) result[key] = [];
    result[key].push(n);//from w  w w .  jav a 2  s .c o  m
  });

  return result;
};



PreviousNext

Related