1

So I have a multidimensional array like:

myArr = [["venue",2],["venue",16],["inning",2],["inning",4],["inning",32],["hithard", 4]]

I would like to add the similar values up. So in the end I just have:

"venue" = 18, "inning" = 38, and "hithard" = 4.

Can you give me an example of how to accomplish this? Either with Javascript and/or jQuery

Thanks!

Ram
  • 143,282
  • 16
  • 168
  • 197
ksumarine
  • 782
  • 2
  • 12
  • 33

4 Answers4

3
myArr = [["venue",2],["venue",16],["inning",2],["inning",4],["inning",32],["hithard", 4]];
values = {};
for (i=0;i<myArr.length;i++){
   if ("undefined" == typeof values[myArr[i][0]]) {values[myArr[i][0]] = 0;}
   values[myArr[i][0]] += myArr[i][1];
}
arr = [];
query_string = "";
for (i in values) {
    // if you want it in an array:
    arr.push('"' + i + '" = ' + values[i]);
    query_string += (query_string.length ? "&" : "") + i + "=" + values[i];
}
​console.log(arr);​

DEMO: http://jsfiddle.net/Ta97E/2/

you can use values to create the query string

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • what's wrong with it? This is dynamic and it sums them by category – Gavriel May 07 '12 at 19:44
  • 1
    I see what you are going for here Gavriel. This solution goes toward the idea of not limiting your code to a static requirement. What if a fourth or fifth statistic was needed? by creating the object based on the key and then incrementing into that key value pair, this solves the problem. I would have approached it only slightly different. This does not deserve a down vote. – C.S. May 07 '12 at 19:45
  • @MikeRobinson Where in the question does he specify that the answer has to be an array result? That is a terrible assumption. The question effectively asks, "how do I add these three things up". – C.S. May 07 '12 at 19:49
  • @ChristopherSmithson You have a point, I was just going with the idea of maintaining datatypes just in case there's part of the code further on that rely on this configuration. – Mike Robinson May 07 '12 at 19:51
3

I am not sure if you want an array or object. If object, stop it is 1st pass and tmp in below code should return you the object as Object { venue=18, inning=38, hithard=4}.

DEMO

var tmp = {}, keys;
for (var i = 0; i < myArr.length; i++) {
    keys = myArr[i][0];
    tmp[keys] = (tmp.hasOwnProperty(keys))? 
              (tmp[keys] + myArr[i][1]):myArr[i][1];
} //tmp - will return you a Object { venue=18, inning=38, hithard=4}

var output = [];
for (keys in tmp) {
    output.push([keys, tmp[keys]]);
} //output will return you an array as [["venue", 18],["inning", 38],["hithard", 4]]     
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • this doesn't have query string output, mine below does – Gavriel May 07 '12 at 20:00
  • @Gavriel OP mentions that he will be using it in query string. However initial post doesn't say anything about query string. Let say if he want this in AJAX data, then he can just use it right away.. – Selvakumar Arumugam May 07 '12 at 20:03
2

Check this code:

var final = {};
for (var i in myArr) {
    var item = myArr[i];
    final[item[0]] = (final[item[0]] || 0) + item[1];
}

console.log(final);​

DEMO: http://jsfiddle.net/UVJEb/

VisioN
  • 143,310
  • 32
  • 282
  • 281
2

Underscore solution:

 sums = _.reduce(myArr, function (obj, item) { 
    obj[item[0]] = (obj[item[0]] || 0) + item[1]; 
    return obj; 
 }, {});

 // sums = {"venue":18,"inning":38,"hithard":4}

A little dirtier in jQuery

sums = {}

$.each(myArr, function (i, value) {
    sums[value[0]] = (sums[value[0]] || 0) + value[1];
});

Edit: add jQuery version

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79