Iterate through multidimensional arrays using for loop

I understand that the code below evaluates multiplication of a simple array

var arr = [[2,3],[2,3]];
var product = 1;

For (i=0; i<arr.length; i++) {
    For (j=0; j<arr[i].length; j++) {
        product *=arr[i][j];

How would you multiply an array like this:
[[2,3,[2,3]], [2,3], [2,3]]?
I’ve tried this, but it doesn’t work.

var arr =[[2,3,[2,3]], [2,3], [2,3]];
var product = 1;

For (i=0; i<arr.length; i++) {
    For (j=0; j<arr[i].length; j++) {
    For (k=0; k<arr[j].lenght; k++) {
        product *=arr[i][j][k];  

Do you have a link to the challenge?

That’s the link to the original challenge, which I have no issue with.
What I’m asking is slightly different.

[[1,2], [3,4], [5,6]]…original challenge.

[[1,2, [2,3]], [3,4], [5,6]]…my own question.
How do you find the product using “for” loop?

Honestly, the easiest way that comes to mind would be to use to flatten the array and then multiply the contents. There is a later change on using recursion to flatten arrays if you wanted to use only loops and recursion to do it instead of built-in methods.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


@JeremyLT is right. If you don’t know in advance how nested your array is, you need a more complicated solution. There are array methods (like flat and reduce) that handle a lot of the messy parts of operations like this for real-world use.

1 Like

Oh wow. Thanks alot. I’m still studying. I’ll keep a keen eye on excursions, then return to this. Really appreciate.

Alright. Thanks and noted.

Thanks so much. I’ll keep this in mind once I’m done studying recursions.

Could you inform me of some built in methods too?

This is the method that comes to mind.

It returns a single array with all of the values.

As said it can quickly become a bit of a hardcoded solution if it is just written for that specific case.

You can check if arr[i][j] is an array, if it is, loop it and do the multiplication of arr[i][j][k], otherwise do the multiplication of arr[i][j].

This might just be garbage code, it definitely is ugly, but it seems to work. I haven’t written a for loop this nested in some time (I can barely remember the last time I wrote a for loop).

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

var product = 1;

for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    if (Array.isArray(arr[i][j])) {
      for (let k = 0; k < arr[i][j].length; k++) {
        product *= arr[i][j][k];
    } else {
      product *= arr[i][j];

console.log(product); // 38880
1 Like

Thanks. I can see that you multiplied the deepest nested arrays first (cause .isarray will be true) . The product then serves as the base value for the next multiplication. Really appreciate your time. I’ll look more into it. Then if possible, try to produce a modified solution.

Although a bit of a hack, you could:

  1. Coerce the array into a string
  2. Remove all the square bracket and space characters (creates a string like '2,3,2,3,2,3,2,3'
  3. Split the resulting string into an array
  4. Use reduce to iterate through the array and multiple all the numbers

I saw a pretty great post yesterday where someone used regex to flatten an array. I was impressed at the ingenuity.

1 Like

My version is much simpler than that post. You can simply concatenate an empty string to the array, replace all square brackets and space characters, split the array using the comma character as the delimiter, then reduce the array. My suggestion assumes you are only dealing with arrays of numbers though.

Sure. I don’t do regex so I wouldn’t know. I was just sharing a related post.

Update for anyone who cares
Here’s a much simpler and straightforward way.

let array  = [[1, 2 , [5, 6]], [3, 4]];
let arrayFlat = array.flat(Infinity) //Flatten multidimensional array of any depth. Infinity is the depth.

//you can now perform anything on the flattened array
let product = arrayFlat.reduce((acc, value) => acc * value,  1)

To be fair, the title of the thread is Iterate through multidimensional arrays using for loop, which you are not doing. You are just flattening the arrays. Not saying it’s a bad choice, just that it isn’t really what was asked for in the thread.

1 Like

Yes I understand that it’s not what was asked. I just wanted to share this to show that it could be done much easily “for anyone who cared know”. Using For loop can become very complex especially if the depths of the nested arrays are not uniform. Thanks for your input.