Flattening Nested Arrays in JavaScript
Nested arrays, arrays within arrays, are a common data structure. The ability to flatten these nested structures into a single, one-dimensional array is a frequently required task in data processing and manipulation. This challenge will test your ability to recursively traverse and combine array elements.
Problem Description
You are tasked with writing a JavaScript function called flattenArray that takes a potentially deeply nested array as input and returns a new, one-dimensional array containing all the original elements in the same order. The function should handle arrays containing any combination of primitive values (numbers, strings, booleans, null, undefined) and other arrays. The function should not modify the original input array.
Key Requirements:
- The function must accept an array as input.
- The function must return a new, flattened array.
- The function must handle arbitrarily deep nesting.
- The function must preserve the original order of elements.
- The function should handle empty arrays gracefully.
Expected Behavior:
The function should iterate through the input array. If an element is an array, it should recursively call flattenArray on that element and concatenate the result. If an element is not an array, it should be added directly to the flattened array.
Edge Cases to Consider:
- Empty input array: Should return an empty array.
- Array with no nesting: Should return a copy of the original array.
- Array with mixed data types: Should handle numbers, strings, booleans, null, and undefined correctly.
- Very deeply nested arrays: Consider potential stack overflow issues (though this is less of a concern in JavaScript due to its tail-call optimization in some engines, it's good to be aware of).
Examples
Example 1:
Input: [1, [2, [3, 4], 5], 6]
Output: [1, 2, 3, 4, 5, 6]
Explanation: The function recursively traverses the nested arrays, extracting each element and adding it to the flattened array in the order it's encountered.
Example 2:
Input: [1, 2, 3]
Output: [1, 2, 3]
Explanation: The input array is already flat, so a copy of the original array is returned.
Example 3:
Input: []
Output: []
Explanation: An empty array is returned as the input is empty.
Example 4:
Input: [1, [2, "hello", [true, null]], undefined]
Output: [1, 2, "hello", true, null, undefined]
Explanation: Handles mixed data types correctly.
Constraints
- The input will always be an array.
- The array can contain any combination of primitive values and other arrays.
- The maximum depth of nesting is not explicitly limited, but extremely deep nesting might impact performance.
- The function should be reasonably efficient for arrays of moderate size (up to 1000 elements).
Notes
Consider using recursion to solve this problem. A base case for the recursion is when an element is not an array. You can use the Array.isArray() method to check if an element is an array. Remember to create a new array to store the flattened result; do not modify the original input array. Think about how to concatenate arrays efficiently in JavaScript.