Flatten Array
Explore how to flatten arrays with multiple nesting levels by applying recursion in JavaScript. Understand step-by-step solutions that preserve order and analyze time and space complexity to write efficient code.
We'll cover the following...
We'll cover the following...
Flatten Array
Instructions
Write a function that will take an array of deeply nested arrays and extract every item, flattening the array. It should return a new array that contains the items of each internal array, preserving order.
Input: Array
Output: Array
Examples:
flatten([ [ [ [1], 2], 3], [4], [], [[5]]]);
// -> [1, 2, 3, 4, 5]
flatten(['abc', ['def', ['ghi', ['jkl']]]]);
// -> ['abc', 'def', 'ghi', 'jkl']
Hints
- As in the last problem, we have to process every item we receive. There’s no way to get around that so the best time complexity we