1

How do we sum each array element in multiple arrays

ex : [[2, 1], [1, 4]]
output : [3], [5]

In this code is the sum based on the index in each array

const in_out = [
  [2, 1],
  [1, 4]
]

function calculate(arr) {
  a = arr.reduce((prev, curr) => prev + curr[idx = 0], 0)
  b = arr.reduce((prev, curr) => prev + curr[idx = 1], 0)
  c = [a + b]
  return c
}

console.log(calculate(in_out))

and what I want is summation based on each array, and dynamic "in_out" variable like this

[2, 1] = 3 and [1, 4] = 5

console.log(calculate (in_out [[2, 1], [1, 4]] ) );

and the output

[8]
5
  • stackoverflow.com/questions/39492385/… |It's a duplicate I guessed Commented Jan 12, 2022 at 6:21
  • Your array is alway 2 dimensional array? Commented Jan 12, 2022 at 6:22
  • @SaeedShamloo yes Commented Jan 12, 2022 at 6:24
  • function calculate(arr) { return arr.map(sub =>[sub.reduce((acc,num)=>acc+num)]) } does this solve your problem? Commented Jan 12, 2022 at 6:37
  • @SaeedShamloo yes, its helped me alot, but when im trying to add console.log(calculate (in_out [[2, 1], [1, 4]] ) ); it doesnt work Commented Jan 12, 2022 at 6:46

4 Answers 4

1

Use Array.reduce() method 2 times, the outer one will give you the transformed array and the nested one will give you the sum.

const list = [
  [2, 1],
  [1, 4]
];

function calculate(arr) {
  return arr.reduce((acc, items) => acc.concat(items.reduce((sum, curr) => sum + curr, 0)), [])
}

console.log(calculate(list))

Sign up to request clarification or add additional context in comments.

Comments

0

Conventional way!

function sumTwoDimensi(arr){
  var arrSum = []; 

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

  return arrSum
}

Then to calculate all of it, just this function:

function sumArr(ar) {
  var sum = 0;
  for (var i = 0; i < ar.length; i++) {
    sum += ar[i];
  }
  return sum;
}

Example for use it:

console.log(sumTwoDimensi(in_out));
console.log(sumArr(sumTwoDimensi(in_out)));

Just copy it!

6 Comments

and how to summarize all value? ex: 3 + 5 =8
i have edit it, sorry i missed on it. U can try it now
when im changing the operator "+" to "-", values are changed to negative not substract ex: [2 - 1] it should be "1" not "-3" can you explain to me? why this happen?
Because this is only for sum, i think you need use another function for substraction.. the explanation for it, because the initialize of sum is 0, it will be [ 0 - firstArray], it will be wrong answer
But it will be trouble when we have array like this [[3, 4, 5], [5, 7]]
|
0

You can try something like this

function in_out(arr){
  var output = []
  arr.map(arrElement=>{
     var sum = 0;
     arrElement.map(el=>{
        sum += el
     })
     output.push([sum])
  })
  return output
}
console.log(in_out([[2,1],[1,4]]))

Comments

0

Just map and reduce:

const in_out = [[2, 1],[1, 4],[1,2,3,4],[],[9,8,7,6],[9]];

const calculate = (data) => 
  data.map((arr) => 
    arr.reduce((sum, num) => sum + num, 0)
  );
  
console.log(calculate(in_out));
.as-console-wrapper{min-height: 100%!important; top: 0}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.