Hone logo
Hone
Problems

Flatten a Nested Array in JavaScript

Arrays in JavaScript can be nested, meaning an array can contain other arrays, which can also contain arrays, and so on. This can make it difficult to process elements uniformly. Your task is to write a JavaScript function that takes a nested array and returns a new, flattened array containing all elements from the original nested structure. This is a common operation when dealing with data from various sources or when preparing data for certain algorithms.

Problem Description

You need to implement a JavaScript function called flattenArray that accepts a single argument: nestedArray. This function should return a new array that contains all the elements from nestedArray, but with any nested arrays "unpacked" into the main array. The order of elements in the flattened array should be preserved as they appear in the original nested structure.

Key Requirements:

  • The function must handle arbitrary levels of nesting.
  • The function should return a new array; it should not modify the original nestedArray.
  • The function should correctly handle non-array elements within the nested structure.
  • The order of elements must be maintained.

Expected Behavior:

If the input array contains only primitive types (numbers, strings, booleans, null, undefined), the output should be the same array. If it contains nested arrays, those nested arrays' contents should be inserted into the output array at their respective positions.

Edge Cases to Consider:

  • An empty input array.
  • An array containing only empty nested arrays.
  • An array with mixed primitive types and nested arrays at various depths.

Examples

Example 1:

Input: [1, 2, [3, 4, [5, 6]], 7]
Output: [1, 2, 3, 4, 5, 6, 7]
Explanation: The nested arrays `[3, 4, [5, 6]]` and `[5, 6]` are unpacked, and their elements are added to the main array in order.

Example 2:

Input: [[1, 2], [3, [4]], 5]
Output: [1, 2, 3, 4, 5]
Explanation: Multiple levels of nesting are handled.

Example 3:

Input: [1, [2, [3, [4, [5]]]]]
Output: [1, 2, 3, 4, 5]
Explanation: Deeply nested structure is flattened correctly.

Example 4:

Input: []
Output: []
Explanation: An empty array should result in an empty flattened array.

Example 5:

Input: [1, 'hello', null, [true, undefined]]
Output: [1, 'hello', null, true, undefined]
Explanation: Primitive types are preserved, and nested arrays are flattened.

Constraints

  • The input nestedArray will always be an array.
  • The elements within the arrays can be any JavaScript data type, including other arrays.
  • The maximum depth of nesting is not specified, implying your solution should be robust for any reasonable depth.
  • Your solution should aim for reasonable performance, avoiding excessively inefficient recursive calls or data structure manipulations.

Notes

Consider how you might iterate through the array and identify elements that are themselves arrays. Recursion is a natural approach for handling arbitrary nesting depths. Alternatively, you might explore iterative solutions using a stack. Remember to create a new array for the result. The built-in Array.prototype.flat() method can solve this, but for this challenge, you are expected to implement the logic yourself.

Loading editor...
javascript