jquery 数组分组依据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14427659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:34:14  来源:igfitidea点击:

jquery array group by

jqueryarrays

提问by InTry

I have array like this

我有这样的数组

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]]

how can I group and sum values by A, B, C...

如何按 A、B、C 对值进行分组和求和...

$.each(abcArr , function() {
    if (this[0] == this[0]) { 
      this[1] + this[1] 
    }; // I know this will simple double [1] values :(
});

desired result should be

想要的结果应该是

[["A", 40], ["B", 20], ["C",30]]

回答by jfriend00

Here's a plain javascript way to do it that collects the unique indexes in a map and totals them as it does, then regenerates the array with the totals:

这是一个简单的javascript方法,它收集地图中的唯一索引并按原样对它们进行总计,然后使用总计重新生成数组:

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]];

var items = {}, base, key;
for (var i = 0; i < abcArr.length; i++) {
    base = abcArr[i];
    key = base[0];
    // if not already present in the map, add a zeroed item in the map
    if (!items[key]) {
        items[key] = 0;
    }
    // add new item to the map entry
    items[key] += base[1];
}

// Now, generate new array
var outputArr = [], temp;
for (key in items) {
    // create array entry for the map value
    temp = [key, items[key]]
    // put array entry into the output array
    outputArr.push(temp);
}

// outputArr contains the result

Working demo: http://jsfiddle.net/jfriend00/vPMwu/

工作演示:http: //jsfiddle.net/jfriend00/vPMwu/



Here's a way using jQuery's .each:

这是使用 jQuery 的一种方法.each

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]];

var items = {}, base, key;
$.each(abcArr, function(index, val) {
    key = val[0];
    if (!items[key]) {
        items[key] = 0;
    }
    items[key] += val[1];
});

var outputArr = [];
$.each(items, function(key, val) {
    outputArr.push([key, val]);
});

// outputArr contains the result
document.body.innerHTML = JSON.stringify(outputArr);

Working demo: http://jsfiddle.net/jfriend00/Q8LLT/

工作演示:http: //jsfiddle.net/jfriend00/Q8LLT/

回答by John Dvorak

Underscore.jshas groupBy, and the rest can be done by mapping over reduce(that is, reducing each group to one element

Underscore.jsgroupBy, 剩下的可以通过mapping over来完成reduce(也就是将每一组减为一个元素

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]]

var grouped = _.groupBy(abcArr, function(x){return x[0]})
var result  = _.map(grouped, function(x){
    return _.reduce(x, function(x,y){
        return [y[0], x[1]+y[1]];
    }, [,0])
})

http://jsfiddle.net/srvKQ/

http://jsfiddle.net/srvKQ/

Or (minified)

或(缩小)

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]]

var result = _.map(
    _.groupBy(abcArr, function(x){return x[0]}),
    function(x){return _.reduce(
        x, function(x,y){return [y[0], x[1]+y[1]]}, [,0]
)});

http://jsfiddle.net/srvKQ/1/

http://jsfiddle.net/srvKQ/1/

回答by Ravi Makwana

You can use Array.reduce()and Map().

您可以使用Array.reduce()Map()

var abcArr= [["A", 10], ["B", 20], ["A",30],["C",40]]

var groupValues = abcArr.reduce(function (obj, item) {
    obj[item[0]] = obj[item[0]] || [];
    obj[item[0]] = +obj[item[0]] + +item[1]; // add (+) before variable so it's cast to numeric 
    return obj;
}, {});

var groupData = Object.keys(groupValues).map(function (key) {
    return [key, groupValues[key]];
});

console.log(groupData)