jlmmns
jlmmns

Reputation: 845

Javascript add 2nd array values together if first values are the same

Sorry if the title isn't that clear. This is kind of hard to explain for me, as I'm not that good when it comes to multiple arrays.

I have an array: [ [21, 1000], [21, 500], [18, 100], [18, 200] ]

Now I want to get this resulting array: [ [21, 1500], [18, 300] ]

How do I go about doing this?

I've tried using 21 and 18 as array keys, but this way I couldn't use the array in a for loop with .length counting. I ended up with a total of 4 arrays to almost get it working.


Edit:

I want to avoid using the 21 and 18 values as array keys.

I also want to be able to iterate over the resulting array/object, and use the 21 and 18 "keys" as well, to make some further changes to my page.

Thanks already for all the responses!

Upvotes: 1

Views: 227

Answers (5)

Xotic750
Xotic750

Reputation: 23482

Here is another possibility, it is using Array.forEach, so you need to have a modern browser or use one of the many "shims"/"polyfills" out there.

Javascript

var first = [21, 18],
    second = [
        [21, 1000],
        [21, 500],
        [18, 100],
        [18, 200]
    ],
    store = {};
    result = [];


first.forEach(function (element) {
    second.forEach(function (pair) {
        var value;

        if (pair[0] === element) {
            value = store[element] || 0;
            store[element] = value + pair[1];
        }
    });

    if (typeof store[element] === "number") {
        result.push([element, store[element]]);
    }
});

console.log(result);

On jsfiddle

Update: here is a solution based on the updated question, this also uses Object.keys and Array.map to do the conversion from an object to the array format that the OP has requested.

Javascript

var first = [
        [21, 1000],
        [21, 500],
        [18, 100],
        [18, 200]
    ],
    store = {};
    result;

first.forEach(function (pair) {
    store[pair[0]] = (store[pair[0]] || 0) + pair[1];
});

result = Object.keys(store).map(function (key) {
    return [parseInt(key, 10), store[key]];
});

console.log(result);

On jsfiddle

Upvotes: 0

jterry
jterry

Reputation: 6269

You definitely don't need any jQuery for this to get the result array you asked for:

var one = [21, 18];
var two = [ [21, 1000], [21, 500], [18, 100], [18, 200] ];
var results = [];

for(var i in one) {
    results[i] = 0;

    for(var x in two)
        if(two[x][0] === one[i])
            results[i] = results[i] + two[x][1];

    results[i] = [one[i], results[i]];
}

Fiddle here

Upvotes: 2

Tracker1
Tracker1

Reputation: 19344

function aggregateResults(indexes, inputArray) {
  var ret = [], tmp;
  for (var i=0; i<indexes.length; i++) {
    tmp = 0;
    for (var j=0; j<inputArray.length; j++) {
      tmp += inputArray[j][0] == indexes[i] ? inputArray[j][1] : 0;
    }
    ret.push([indexes[i], tmp])
  }
  return ret;
}

Upvotes: 0

carrabino
carrabino

Reputation: 1762

in case your values are not in order, you can use this:

fiddle:
http://jsfiddle.net/acturbo/YWaja/

javasript:

var array1=    [21, 18];
var array2 =   [ [21, 1000], [21, 500], [18, 100], [18, 200] ];
var result =   [ [0,0], [0,0] ];

for (i=0;i<array1.length;i++){

    result[0,i][0] = array1[i];

    for (j=0;j<array2.length;j++){

        var key = array2[i,j][0];
        var val = array2[i,j][1];

        if (  key == array1[i] ){
            result[0,i][1] += val;
        }
    }
}

Upvotes: 0

adeneo
adeneo

Reputation: 318212

How about creating an object :

var arr = [ [21, 1000], [21, 500], [18, 100], [18, 200] ];
    obj = {};

for (i=0; i<arr.length; i++) {
    obj[arr[i][0]] = (obj[arr[i][0]] || 0) + arr[i][1];
}

// obj now equals {18: 300, 21: 1500} 
// obj[21] equals 1500 etc

FIDDLE

You could create an array the same way :

var arr  = [ [21, 1000], [21, 500], [18, 100], [18, 200] ],
    arr2 = [];

for (i=0; i<arr.length; i++) {
    arr2[arr[i][0]] = (arr2[arr[i][0]] || 0) + arr[i][1];
}

// giving you the array [18: 300, 21: 1500]
// where arr2[18] equals 300

Upvotes: 6

Related Questions