Nodejs Array Group By groupBy(fieldName)

Here you can find the source of groupBy(fieldName)

Method Source Code

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 = {};//from  ww w  . ja  va2  s .  c  om
        uniqVal.map(item => tempObj[item] = []);
        this.forEach(item => {
            for (let name in tempObj) {
                if (item[fieldName] == name) {
                    tempObj[name].push(item);
                }
            }
        });
        return tempObj;
    } else {
        console.log('Not exist field:', fieldName);
        return this;
    }
    ;
};

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(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){
    ...
    
  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
      }, {})
    ...
    
  7. groupBy(funcProp)
    Array.prototype.groupBy = function (funcProp) {
      return this.reduce(function (accumulator, value) {
        (accumulator[funcProp(value)] = accumulator[funcProp(value)] || []).push(value);
        return accumulator;
      }, {});
    };
    Array.prototype.orderBy = function (funcProp, desc = false) {
      return this.sort((a, b) => {
        var aValue = funcProp(a);
    ...