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…
$.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]]
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:
Working demo: http://jsfiddle.net/jfriend00/vPMwu/
Here’s a way using jQuery’s
.each:Working demo: http://jsfiddle.net/jfriend00/Q8LLT/