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]