Problem
Given an nested array, flatten it to 1-dimension array.
For example:
1 | [1, [2, [ [3, 4], 5], 6], [7, 8], 9] |
Solution 1: iterative version
1 | // 1. iterative [stack] |
Solution 2: recursive version
1 | function flatten2(arr){ |
Solution 3: recursive version [shorter]
1 | function flatten(a) { |
Testing
1 | let test1 = [1, [2, 3, [4, 5]], 6, [7, [8, 9], 10], 11] |