Javascript Array groupBy(iterator, context)

Description

Javascript Array groupBy(iterator, context)


Array.prototype.groupBy = Array.prototype.groupBy || function(iterator, context) {
  iterator = typeof iterator === 'function' ? iterator : function(value) {return value;};

  function group(groups, value) {
    var groupKey = iterator.call(context, value);

    (groups[groupKey] = groups[groupKey] || []).push(value);

    return groups;
  }/*from  w  ww .  j  a  v  a  2s. com*/

  return this.reduce(group, {});
};



PreviousNext

Related