1

I'm using a nested array like this:

const data = [
  [0],
  [2],
  [[1], 3]
  1
]

Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1). And is it also possible to count how many arrays are used? This would be 5 arrays

0

1 Answer 1

8
 const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);

This uses a recursive approach with reduce.

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

2 Comments

Is it possible to count also the arrays?
Yes it is. May try that yourself? ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.