user386430 user386430 - 1 month ago 6
AngularJS Question

How to iterate array inside array in java script

Can anybody tell how to iterate array inside array in java script

eg
Var a =[1,3,[6],7,[8]]

Thanks

Answer

try this

const inputs = [1,3,[6],7,[8]]

/** loop array */
for (const input of inputs) {
  if (Array.isArray(input)) {
    /** input is array, loop nested array */
    for (const nestedInput of input) {
      /** print item of nested array */
      console.log(nestedInput)
    }
  }
  else {
    /** input is number, print it */
    console.log(input)
  }
}

note that: there are so many ways to loop, for..of, forEach, for, while, etc.

Comments