iterating through an array and performing a function

Taiwo Sulaimon

I have my original array like this

[Array(3), Array(1), Array(3)]

each array has this

[2, 2, 3]
[1]
[2, 2, 2]

I want my final result to be something like this

[7,1,6]
Ori Drori

Create a array sum function using Array.reduce(), and then map your array, and apply sum to each sub-array:

const sum = arr => arr.reduce((s, n) => s + n, 0)

const arr = [[2, 2, 3], [1], [2, 2, 2]]

const result = arr.map(sum)

console.log(result)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related