0

I have searched for similar questions on here but the expected result does not meet my needs. Consider the following multidimensional array.

let list = [
   [1,   10,    13,    4],
   [5,   6,    83,    45],
   [9,   18,   11,   12]
];

How would you go about merging all values to produce the following result.

const output = [15, 34, 107, 61];

1 Answer 1

3

You could reduce the array by adding up the numbers:

 list.reduce((a, b) => a.map((n, i) => n + b[i]))

(This will fail if the arrays got different lengths)

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

2 Comments

The good news is that it works, could you give a brief explanation so I can understand why it works?
@jermaine you should read about reduce first, MDN has a good explanation, also this answer might help

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.