Nodejs Array Group By groupBy(fn)

Here you can find the source of groupBy(fn)

Method Source Code

/*/*from   ww  w . ja va  2 s  .  co m*/
Add a groupBy method to Array.prototype 
so that elements in an array could be grouped by the result of evaluating a function on each eleme

The method should return an object, in which for each different value returned by the function 
there is a property whose value is the array of elements that return the same value.

If no function is passed, the element itself should be taken


Algo:
  use reduce

  find keys with acc[fn(x)]
  filter duplicated keys

  find values associated with keys:
    filter array for each value through `fn` equal `

*/

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
  }, {})
}

// console.log([1,2,3,2,4,1,5,1,6].groupBy())
// console.log([1,2,3,2,4,1,5,1,6].groupBy(function(val) { return val % 3;} ))


/*Enlightment:
  

*/

Related

  1. 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);
     });
    ...
    
  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])] = [
    ...
    
  6. 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);
    ...
    
  7. groupBy(groupFunction)
    function groupByArray(array, groupFunction) {
        var map = {}
        array.forEach(
        function forEachFunction(item, index, array) {
            var groupName = groupFunction(item, index, array)
            if (map[groupName]) map[groupName].push(item)
            else map[groupName] = [item]
        })
        return map
    ...
    
  8. groupBy(hash)
    Array.prototype.groupBy = function(hash){
      var _hash = hash ? hash : function(o){return o;};
      var _map = {};
      var put = function(map, key, value){
        if (!map[_hash(key)]) {
            map[_hash(key)] = {};
            map[_hash(key)].group = [];
            map[_hash(key)].key = key;
        map[_hash(key)].group.push(value); 
      this.map(function(obj){
        put(_map, obj, obj);
      });
      return Object.keys(_map).map(function(key){
        return {key: _map[key].key, group: _map[key].group};
      });
    
  9. groupBy(prop)
    Array.prototype.groupBy = function(prop) {
      return this.reduce(function(groups, item) {
        var val = item[prop];
        groups[val] = groups[val] || [];
        groups[val].push(item);
        return groups;
      }, {});
    let students = [
    ...