Underscore.js groupBy multiple values

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

    An example with lodash and mixin

    _.mixin({
    'groupByMulti': function (collection, keys) {
    if (!keys.length) {
     return collection;
     } else {
      return _.mapValues(_.groupBy(collection,_.first(keys)),function(values) {
        return _.groupByMulti(values, _.rest(keys));
      });
    }
    }
    });    
    

提交回复
热议问题