Add specific values of multidimensional array together Javascript -


okay, have multidimensional array contains 9 arrays. each of these nested arrays contains 10 numeric values. sake of simplicity, let's looks this:

var myarray = [ [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10] ] 

i trying write function take first index of each nested array (in case, 1's) , add them together, pushing value either array or object. then, need function continue on, adding values of next index, , next, , on , forth. in end, should have array of 10 values (or object works here well). values be:

1+1+1+1+1+1+1+1+1, 2+2+2+2+2+2+2+2+2, 3+3+3+3+3+3+3+3+3... 

...and on forth, actual values of new array this:

[9, 18, 27, 36, 45, 54, 63, 72, 81] 

the catch here need flexible/dynamic, work in case myarray has 6 arrays, or maybe nested arrays have 4 values each. should work amount of nested arrays, each own amount of values (though each nested array contain same amount of values 1 another!).

what best way accomplish via javascript and/or jquery? note have values output object, in fashion:

{1:9, 2:18, 3:27, 4:36, 5:45, 6:54, 7:63, 8:72, 9:81} 

i tried using similar code stackoverflow thread object, returning

{1:nan, 2:nan, 3:nan, etc.}  

that thread can found here:

javascript multidimensional array: add values

i'm using "underscore" method , jquery $.each part of provided otto.

anyone able here??

here solution:

var myarray = [ [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10], [1,2,3,4,5,6,7,8,9,10] ]  var results= [];  myarray.map(function(a){     for(var i=0;i<a.length;i++){         if(results.length === a.length){             results[i] = results[i] + a[i];         }else{             results.push(a[i]);         }     } }); 

http://jsfiddle.net/umpaa/


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -