Underscore.js groupBy multiple values

前端 未结 9 1437
渐次进展
渐次进展 2020-12-01 05:03

Using Underscore.js, I\'m trying to group a list of items multiple times, ie

Group by SIZE then for each SIZE, group by CATEGORY...

http://jsfiddle.net/ricky

相关标签:
9条回答
  • 2020-12-01 05:16

    I think @Bergi's answer can be streamlined a bit by utilizing Lo-Dash's mapValues (for mapping functions over object values). It allows us to group the entries in an array by multiple keys in a nested fashion:

    _ = require('lodash');
    
    var _.nest = function (collection, keys) {
      if (!keys.length) {
        return collection;
      }
      else {
        return _(collection).groupBy(keys[0]).mapValues(function(values) { 
          return nest(values, keys.slice(1));
        }).value();
      }
    };
    

    I renamed the method to nest because it ends up serving much the same role served by D3's nest operator. See this gist for details and this fiddle for demonstrated usage with your example.

    lodash nest groupby

    0 讨论(0)
  • 2020-12-01 05:21

    Grouping by a composite key tends to work better for me in most situations:

    const groups = _.groupByComposite(myList, ['size', 'category']);
    

    Demo using OP's fiddle

    Mixin

    _.mixin({
      /*
       * @groupByComposite
       *
       * Groups an array of objects by multiple properties. Uses _.groupBy under the covers,
       * to group by a composite key, generated from the list of provided keys.
       *
       * @param {Object[]} collection - the array of objects.
       * @param {string[]} keys - one or more property names to group by.
       * @param {string} [delimiter=-] - a delimiter used in the creation of the composite key.
       *
       * @returns {Object} - the composed aggregate object.
       */
      groupByComposite: (collection, keys, delimiter = '-') =>
        _.groupBy(collection, (item) => {
          const compositeKey = [];
          _.each(keys, key => compositeKey.push(item[key]));
          return compositeKey.join(delimiter);
        }),
    });
    
    0 讨论(0)
  • 2020-12-01 05:23

    A simple recursive implementation:

    _.mixin({
      /*
       * @mixin
       *
       * Splits a collection into sets, grouped by the result of running each value
       * through iteratee. If iteratee is a string instead of a function, groups by
       * the property named by iteratee on each of the values.
       *
       * @param {array|object} list - The collection to iterate over.
       * @param {(string|function)[]} values - The iteratees to transform keys.
       * @param {object=} context - The values are bound to the context object.
       * 
       * @returns {Object} - Returns the composed aggregate object.
       */
      groupByMulti: function(list, values, context) {
        if (!values.length) {
          return list;
        }
        var byFirst = _.groupBy(list, values[0], context),
            rest    = values.slice(1);
        for (var prop in byFirst) {
          byFirst[prop] = _.groupByMulti(byFirst[prop], rest, context);
        }
        return byFirst;
      }
    });
    

    Demo in your jsfiddle

    0 讨论(0)
提交回复
热议问题