Array.prototype.flatadvancedconst arr = [[1, 2], [3, [4, 5]]]
arr.flat(1) // β [1, 2, 3, [4, 5]]
arr.flat(2) // β [1, 2, 3, 4, 5]Tune the depth argument of flat() to precisely control how many nesting levels collapse.
The depth argument to flat() can be any non-negative integer or Infinity. This lets you build configurable flattening utilities.
Challenge: Implement flatToDepth(arr, depth) that wraps arr.flat(depth).
flatToDepth([1, [2, [3]]], 1) // β [1, 2, [3]]
flatToDepth([1, [2, [3]]], 2) // β [1, 2, 3]