Nodejs Array Group By groupBy(prop)

Here you can find the source of groupBy(prop)

Method Source Code

Array.prototype.groupBy = function(prop) {
  return this.reduce(function(groups, item) {
    var val = item[prop];
    groups[val] = groups[val] || [];/*from  w  w  w  .  j  a  v  a2 s . c  o  m*/
    groups[val].push(item);
    return groups;
  }, {});
}

let students = [
  { name: "Jane", module: "4FE", track: "frontEnd" },
  { name: "Jane", module: "3FE", track: "frontEnd" },
  { name: "Jane", module: "3BE", track: "backEnd" },
  { name: "Jane", module: "4FE", track: "frontEnd" },
  { name: "Jane", module: "2BE", track: "backEnd" },
  { name: "Jane", module: "3BE", track: "backEnd" }
];

console.log(students.groupBy('track'))

Related

  1. 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])] = [
    ...
    
  2. 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
      }, {})
    ...
    
  3. 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);
    ...
    
  4. 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
    ...
    
  5. 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};
      });
    
  6. groupBy(selector, comparer)
    Array.prototype.groupBy = function (selector, comparer) {
        var grp = [];
        var l = this.length;
        comparer = comparer || EqualityComparer;
        selector = selector || Selector;
        for (var i = 0; i < l; i++) {
            var k = selector(this[i]);
            var g = grp.first(function (u) { return comparer(u.key, k); });
            if (!g) {
    ...