Underscore.js groupBy multiple values

前端 未结 9 1498
渐次进展
渐次进展 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: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

提交回复
热议问题