2

How can I iterate over nested arrays like this with recursion in javascript:

var a = [10, [1, [2,2,2], 3], 20, 'Hallo']

In Python it looks like this:

def foo1(L):
    for i in L:
        if not isinstance(i, list):
            print(i)
        else:
            foo1(i)

foo1(a)

How can I write if not isinstance(i, list): in javascript?

4

1 Answer 1

0

You can use:

var a = [10, [1, [2,2,2], 3], 20, 'Hallo']

for (let item of a) {
    if (!Array.isArray(item)){
      console.log("Not array", item);
    }else{
      console.log("Array", item);
    }
}

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.