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?
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?
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))