-1

I have output like this(Multi dimensional array);

    (4) [Array(3), Array(2), Array(2), Array(2)]
    0: (3) [9, 8, 9]
    1: (2) [5, 6]
    2: (2) [6, 7]
    3: (2) [4, 4]
    length: 4
    __proto__: Array(0)

I would like to get each value and multiply them and return the value. How do I do that?

Kody R.
  • 2,430
  • 5
  • 22
  • 42
Code
  • 313
  • 1
  • 2
  • 11

1 Answers1

-1

First of all you should probably check why the numbers are coming as an array if you need to join them into one number. But, working with what you've given to us...

First, you have to get each inner array and join it as one single number. The best approach I can imagine is casting each number inside to a string, concatenating them, and casting back to Number, like this:

const numbersArray = outerArray
  .map(innerArray =>
    innerArray.map(number => number.toString()).join(''))
  .map(Number)

After having mapped the array, you can then reduce it to a single number, multiplying along the way (and starting at 1 since we don't want an initial value to change the result):

numbersArray.reduce((product, each) => product * each, 1)
gchiconi
  • 624
  • 1
  • 7
  • 17