Looping through multi-dimensional array in javascript skips data?

logos_164

I have the following array:

let array = [1, 2, 3, [(4, 5, [(6, 7, 8)], 9, [10])]];

I'm using recursion to loop through and reduce it down to a single array.

To start, I just want to console.log(array) within the for loop. So code is:

function reduceArray(array) {
  for (let i = 0; i < array.length; i++) {
    console.log(array[i])
  }
}

Expecting to see 4 elements of:

1
2
3
[(4, 5, [(6, 7, 8)], 9, [10])]

I instead get:

1
2
3
[10]

What's happening here?

junvar

You have parens (), which you seem to assume do nothing, but they actually do have meaning in JS and most languages. Here, the ()'s mean the , is interpreted as comma operators instead of as delimiters for arrays.

  • 3 evaluates to 3, simple enough.
  • [3, 4] interprets the , as an array delimiter and evaluates to an array with 2 elements.
  • (x(), 3, 5) interprets the , as the comma operator and evaluates to 5, but also invokes x(), which may have side affects.
  • [3, (x(), 6)] interprets the 1st , as an array delimiter, interprets the 2nd , as the comma operator, and evaluates to [3, 6], but also invokes x().

Here's another example:

let y = 10;
let incY = () => y++;
console.log((incY(), incY(), y));

So returning to your example, when you have [(4, 5, [(6, 7, 8)], 9, [10])], that evaluates to [10].

The comma operator (,) simply evaluates both of its operands and returns the value of the last operand. MDN, comma operator

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Looping Through Multi-Dimensional Array in PHP

Looping through a multi-dimensional array - PHP

Looping through PHP multi-dimensional array

php looping through array to create multi dimensional array

Looping through multi-dimensional array and filtering based on a condition

Merging and looping multi dimensional array

Looping through multi-dimensional arrays in Java

javascript iterate through and map a multi-dimensional array

Looping through a JavaScript array

Looping through two dimensional array backwards

Incrementing Multi Dimensional Array in JavaScript

Javascript multi dimensional array issue

Problem in Multi dimensional array in javascript

Access Multi dimensional array through a dynamic variable

Iterating through multi dimensional array in Go

Accessing a multi-dimensional array through pointer

Iterating through multi-dimensional array

Javascript looping through an JSON array

Looping through nested array in javascript

JavaScript: Looping through an array of booleans

Looping through a PHP array in javascript

Looping through an array of promises in javascript

Looping thru checked checkboxes and creating multi dimensional array

Convert Array of Objects to Multi Dimensional Array in JavaScript

Flat array to multi dimensional array (JavaScript)

How to parse multi dimensional JSON data that does not have a key through Javascript?

How to convert a deep multi dimensional array to a single dimensional array - Javascript

How to push data in multi dimensional array format

Implode data from a multi-dimensional array