Nodejs Array Group By groupBy(callback)

Here you can find the source of groupBy(callback)

Method Source Code

// write a function on the Array prototype 
// called groupBy(callback) the takes a callback 
// function. For each item in groupBy, callback's 
// arguments will be (value, index, array).
// //from   w w  w . j  a v a2s .  c o m
// The value returned by the callback becomes the 
// key for the original value in a new collection.
// 
// i.e. 
// 
// [1,2,3,4,5,6,7,8,9,10].groupBy(function(v, i, arr){
//      return (v%2 === 0) ? 'even' : 'odd'
// })
// 
// //--> { odd: [1,3,5,7,9], even: [2,4,6,8,10] }

Array.prototype.groupBy = function(callback){
    this.reduce() 
   // ['1', '3', '5', '7', '9'].groupBy(() => 'odd')
   // ['2', '4', '6', '8'].groupBy(() => 'even')
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){
      return even[]
   }else{ return odd[]}

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){
    var results = {};
      this.forEach(function (el){
        var key = callback(el);
        if (key in results === false) {
          results[key] = [];
        results[key].push(el);
     });
    ...
    
  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])] = [
    ...
    
  6. groupBy(fn)
    Array.prototype.groupBy = function(fn) {
      if (!fn) return this.groupBy(x => x)
      return this.reduce((acc, x) => {
        if (acc[fn(x)]) return acc
        acc[fn(x)] = this.filter(i => {
          return fn(x) === fn(i)
        })
        return acc
      }, {})
    ...