Hone logo
Hone
Problems

Implementing Array.prototype.flat in JavaScript

The flat() method is a built-in JavaScript function that creates a new array with all sub-array elements concatenated into it recursively up to a specified depth. This challenge asks you to implement this functionality yourself, providing a deeper understanding of array manipulation and recursion in JavaScript. Implementing flat() is useful for simplifying nested data structures and making them easier to work with.

Problem Description

You are tasked with implementing the Array.prototype.flat() method. This method should take one optional argument, depth, which represents the depth to which the array should be flattened. If depth is not provided, it should default to 1. The method should return a new array containing all elements of the original array and its sub-arrays, flattened to the specified depth.

Key Requirements:

  • Recursion: The flattening process should be recursive to handle arbitrarily nested arrays.
  • Depth Control: The depth argument should control how many levels of nesting are flattened.
  • New Array: The method must return a new array, not modify the original array.
  • Default Depth: If depth is not provided, it should default to 1.
  • Non-Array Elements: Non-array elements within the nested arrays should be included in the flattened array as is.

Expected Behavior:

The method should iterate through the input array. If an element is an array, it should recursively call flat() on that sub-array with a reduced depth. If an element is not an array, it should be added to the resulting flattened array.

Edge Cases to Consider:

  • Empty arrays at any level of nesting.
  • Arrays with no nested arrays.
  • depth values of 0.
  • Very deep nesting (consider potential stack overflow issues, though this is less of a concern for typical use cases).
  • Arrays containing null or undefined values. These should be preserved in the flattened array.

Examples

Example 1:

Input: [1, 2, [3, 4, [5, 6]]];
Output: [1, 2, 3, 4, 5, 6]
Explanation: The array is flattened to a depth of 1 by default.

Example 2:

Input: [1, 2, [3, 4, [5, 6]]], 2;
Output: [1, 2, 3, 4, 5, 6]
Explanation: The array is flattened to a depth of 2.

Example 3:

Input: [1, 2, [3, 4, [5, 6]]], 3;
Output: [1, 2, 3, 4, 5, 6]
Explanation: The array is flattened to a depth of 3 (the maximum possible depth in this case).

Example 4:

Input: [1, [2, [3, [4, [5]]]]], 1;
Output: [1, 2, [3, [4, [5]]]]
Explanation: The array is flattened to a depth of 1.

Example 5:

Input: [1, 2, [3, 4, [5, undefined]]];
Output: [1, 2, 3, 4, 5, undefined]
Explanation: `undefined` values are preserved.

Constraints

  • The input will always be a JavaScript array.
  • The depth argument, if provided, will be a non-negative integer.
  • The performance should be reasonable for arrays with a moderate level of nesting (up to 5 levels). While extreme performance optimization isn't the primary goal, avoid unnecessarily inefficient operations.
  • The method should not modify the original array.

Notes

  • Consider using recursion to traverse the nested arrays.
  • The default value for depth is 1.
  • Think about how to handle the base case for the recursion (when depth reaches 0 or the element is not an array).
  • You can use the Array.isArray() method to check if an element is an array.
  • Remember to create and return a new array.
Loading editor...
javascript