简体   繁体   中英

Sum of two dimensional array

the array[time, value]; I need to total/sum of value from this two dimensional array?

var array =[ 
    [1361824790262, 90.48603343963623],
    [1361828390262, 500.18687307834625],
    [1361831990262, 296.05108177661896], 
    [1361835590262, 423.1198309659958], 
    [1361839190262, 11.86623752117157], 
    [1361842790262, 296.38282561302185], 
    [1361846390262, 424.31847417354584], 
    [1361849990262, 100.07041704654694], 
    [1361853590262, 434.8605388402939],
    [1361857190262, 434.8220944404602],
    [1361860790262, 183.61854946613312]
];
var sum = 0;
//console.log(array.length);
for (var i = 0; i < array.length; i++) {
    //console.log(array[i]);
    for (var j = 0; j < array[i].length; j++) {
        console.log(array[j][i]);
        sum += array[j][i];
    }
}
console.log(sum);

Link for JsFiddle

Your question title implies you want to sum a two dimensional array—here's how you would do that:

array.reduce(function(a,b) { return a.concat(b) }) // flatten array
     .reduce(function(a,b) { return a + b });      // sum

To sum only the value portions as you made clear in your edit is even easier:

array.map(function(v) { return v[1] })         // second value of each
     .reduce(function(a,b) { return a + b });  // sum

There's no need for two loops. This loops through the array and gives you each time/value pair. Simply sum the first index (second item) if each time-value pair.

var sum = 0;
for(var i=0;i<array.length;i++){
    console.log(array[i]);
    sum += array[i][1];
}
console.log(sum);

Output:

[1361824790262, 90.48603343963623] 
[1361828390262, 500.18687307834625]
[1361831990262, 296.05108177661896]
[1361835590262, 423.1198309659958] 
[1361839190262, 11.86623752117157] 
[1361842790262, 296.38282561302185]
[1361846390262, 424.31847417354584]
[1361849990262, 100.07041704654694]
[1361853590262, 434.8605388402939] 
[1361857190262, 434.8220944404602] 
[1361860790262, 183.61854946613312]
3195.7829563617706 
    Sum_of_2d_array(arr)
    {
        int sum_time =0;
        int sum_value=0;

        for(i=0;i<arr.length;i++)
        {
            sum_time = sum_time + arr[i,0];
        }
        print(sum_time);

        for(i=0;i<arr.length;i++)
        {
            sum_value = sum_value + arr[i,1];
        }
        print(sum_value);
    }

    If I understood your question correctly. I think this is what you want.
var arr = [[1361824790262, 90.48603343963623],
[1361828390262, 500.18687307834625],
[1361831990262, 296.05108177661896], 
[1361835590262, 423.1198309659958], 
[1361839190262, 11.86623752117157], 
[1361842790262, 296.38282561302185], 
[1361846390262, 424.31847417354584], 
[1361849990262, 100.07041704654694], 
[1361853590262, 434.8605388402939],
[1361857190262, 434.8220944404602],
[1361860790262, 183.61854946613312]];

var sum = 0;
 for(var i=0;i<arr.length;i++){
    for(var j=0;j<arr[i].length;j++){
        sum += arr[i][j];
    }
}
console.log(sum);

This is the solution for get the total. I have test it.

  1. Loop Through The Main Array.
  2. Loop Those Nested Arrays to access The numbers.
  3. Add Those Numbers in a Variable. (Sum)
function sum(array) {
  let sum = 0

  for (let i = 0; i < array.length; i++) {
    for (let j = 0; j < array[i].length; j++) {
      sum = sum + array[i][j]
    }
  }
  return sum
}

console.log(
  sum([
    [1, 1],
    [2, 2],
    [5, 5],
  ])
) //22

That's It:) Have a Good day!

const array2d = [[1, 2], [3, 4]];
array2d.flat().reduce((sum, currentValue) => sum + currentValue); // 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM