Group by array by accumulator - Node.js Array

Node.js examples for Array:Array Operation

Description

Group by array by accumulator

Demo Code


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  ww  . j a v a  2 s  . c  om*/
  });

  return result;
};

Related Tutorials