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


sorry if title isn't clear. kind of hard explain me, i'm not when comes multiple arrays.

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

now want resulting array: [ [21, 1500], [18, 300] ]

how go doing this?

i've tried using 21 , 18 array keys, way couldn't use array in for loop .length counting. ended total of 4 arrays working.


edit:

i want avoid using 21 , 18 values array keys.

i want able iterate on resulting array/object, , use 21 , 18 "keys" well, make further changes page.

thanks responses!

you don't need jquery result array asked for:

var 1 = [21, 18]; var 2 = [ [21, 1000], [21, 500], [18, 100], [18, 200] ]; var results = [];  for(var 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


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -