本文介绍了Underscore.js GROUPBY多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Underscore.js

,我想组项目列表多次,即

按大小,然后每个尺寸,按类别组...

在理想情况下,我想有一个函数或扩大这样就可以在它与PARAMATERS通过抛出一个阵列组。

VAR多组= ['大小','分类'];

或许可以只让一个mixin ...

_。混入({
    groupByMulti:函数(OBJ,缬氨酸,编曲){
        VAR的结果= {};
        VAR迭代器= typeof运算VAL ==功能? VAL:函数(OBJ){
                返回OBJ [VAL]
            };
        _.each(ARR,功能(arrvalue,arrIndex){
            _.each(OBJ,功能(价值,objIndex){
                VAR键=迭代器(价值,objIndex);
                VAR arrresults = OBJ [objIndex] [arrvalue];
                如果(_.has(值,arrvalue))
                    (结果[arrIndex] ||(结果[arrIndex] = [])),推(值);

我的头好痛,但我认为一些推动需求何去何从...

});
        })
        返回结果;
    }
});性能= _.groupByMulti(性能,功能(项目){    名为testVal VAR =项目[大小];    如果(parseFloat(名为testVal)){
        =名为testVal parseFloat(项目[大小])
    }    名为testVal返回},多组);


解决方案

一个简单的递归实现:

_。混入({
  / *
   * @mixin
   *
   *拆分集合成组,通过运行各值的结果分组
   *通过iteratee。如果iteratee是一个字符串代替的功能,基团通过
   *通过iteratee上的每个值命名的属性。
   *
   * @参数数组{|}对象列表 - 收集遍历。
   * @参数{(字符串|功能)[]}值 - 转换键的iteratees。
   * @参数{对象=}上下文 - 该值绑定到上下文对象。
   *
   * @Returns {}对象 - 返回组成聚合对象。
   * /
  groupByMulti:功能(列表,价值观,上下文){
    如果(!values​​.length){
      返回列表;
    }
    VAR byFirst = _.groupBy(列表,值[0],上下文),
        其余= values​​.slice(1);
    对(在byFirst VAR道具){
      byFirst [道具] = _.groupByMulti(byFirst [道具],休息,背景);
    }
    返回byFirst;
  }
});

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/rickysullivan/WTtXP/1/

Ideally, I'd like to have a function or extend _.groupBy() so that you can throw an array at it with the paramaters to group by.

var multiGroup = ['size', 'category'];

Probably could just make a mixin...

_.mixin({
    groupByMulti: function(obj, val, arr) {
        var result = {};
        var iterator = typeof val == 'function' ? val : function(obj) {
                return obj[val];
            };
        _.each(arr, function(arrvalue, arrIndex) {
            _.each(obj, function(value, objIndex) {
                var key = iterator(value, objIndex);
                var arrresults = obj[objIndex][arrvalue];
                if (_.has(value, arrvalue))
                    (result[arrIndex] || (result[arrIndex] = [])).push(value);

My head hurts, but I think some more pushing needs to go here...

            });
        })
        return result;
    }
});

properties = _.groupByMulti(properties, function(item) {

    var testVal = item["size"];

    if (parseFloat(testVal)) {
        testVal = parseFloat(item["size"])
    }

    return testVal

}, multiGroup);
解决方案

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

这篇关于Underscore.js GROUPBY多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:23