Nodejs Array Group By groupBy(callback)

Here you can find the source of groupBy(callback)

Method Source Code

Array.prototype.groupBy = function(callback){
var results = {};
  this.forEach(function (el){
    var key = callback(el);
    if (key in results === false) {
      results[key] = [];//from   w w  w  .  j a  v a2  s .c  o m
    }
    results[key].push(el);
 });
  return Object.keys(results).map(function(key) {
    return {
      key: key,
      values: results[key]
    };
  });
};

var grouped = [1,2,3,4,5,6,7,8,9,10].groupBy(function(v, i, arr){
  return (v % 2 === 0) ? 'even' : 'odd';
});

console.log(grouped);

Related

  1. groupBy(callback)
    Array.prototype.groupBy = function(callback) {
        let obj = {}
        this.forEach(el => {
            let index = callback(el)
            if (!obj.hasOwnProperty(index)) {
                obj[index] = []
            obj[index].push(el)
        })
    ...
    
  2. groupBy(callback)
    Array.prototype.groupBy = function(callback){
        this.reduce() 
    var result{}
    this.forEach(v, i, arr) = function(){}
    v, i, arr
    Array.prototype.groupBy = function(callback){ 
      var evens = Array.groupBy.forEach([9])
      if(Array.groupBy %2 === 0){
    ...
    
  3. groupBy(fieldName)
    Array.prototype.groupBy = function (fieldName) {
        if (this.some(item => item.hasOwnProperty(fieldName))) {
            let uniqVal = this.map(item => item[fieldName]).filter((value, item, array) => array.indexOf(value) === item);
            let tempObj = {};
            uniqVal.map(item => tempObj[item] = []);
            this.forEach(item => {
                for (let name in tempObj) {
                    if (item[fieldName] == name) {
                        tempObj[name].push(item);
    ...
    
  4. groupBy(fieldkey,fieldsums,fn)
    Array.prototype.groupBy=function(fieldkey,fieldsums,fn){
      var groups = _.groupBy(this,function(item){
        return item[fieldkey];
      });
      async.map(_.keys(groups)
        ,function(key,callback){
          var value = groups[key];
          var r = {};
          r[fieldkey] = key;
    ...
    
  5. groupBy(fn)
    Array.prototype.groupBy = function (fn) {
      var result = {
      };
      for (var i = 0; i < this.length; i++)
        try {
          if (!result[fn(this[i])])
            result[fn(this[i])] = [
    ...