Ask Your Question
1

What is the way to add up items in an array with two dimensions using Google Apps Script?

asked 2021-10-01 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-10-05 08:00:00 +0000

qstack gravatar image

One way to add up items in an array with two dimensions using Google Apps Script is to use a nested for loop. Here is an example:

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

// Example usage
var myArray = [[1, 2], [3, 4], [5, 6]];
var total = sumArray(myArray);
Logger.log(total); // Output: 21

In this example, the sumArray function takes an array with two dimensions as its argument. The function uses a nested for loop to iterate over each item in the array, and adds each item to the sum variable. Finally, the function returns the total sum of all the items in the array.

To use this function with your own array, simply replace myArray with your array variable in the example usage.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-01 11:00:00 +0000

Seen: 16 times

Last updated: Oct 05 '22