-2

How do I get a nested list like this :

[1, 2, 3, [4, 5, [6, [7,8], 9]], 0]

to show like this => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

using recursion?

Can anyone help?

  • See [Why is “Can someone help me?” not an actual question?](//meta.stackoverflow.com/q/284236/4642212). Stack Overflow is not a free code-writing service. What have you tried so far? – Sebastian Simon Jan 01 '22 at 15:51

1 Answers1

0

You can use flat(). flat only removes one level of nested array by default. However, you can specify how much nested levels you want to remove in parameter. In your case, 3 does the trick.

const a = [1, 2, 3, [4, 5, [6, [7,8], 9]], 0]

function flatten(arr){
  return arr.flat(3)
}

console.log(flatten(a))
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114