FunctionIdentifies the base type of array elements
The array or array-like object to flatten
Optionaldepth: numberThe flattening depth, defaults to 1. Use Infinity for complete flattening
A new flattened array
arrFlatten([1, [2, 3], [4, [5, 6]]]); // [1, 2, 3, 4, [5, 6]]
arrFlatten([1, [2, 3], [4, [5, 6]]], 2); // [1, 2, 3, 4, 5, 6]
arrFlatten([1, [2, 3], [4, [5, 6]]], Infinity); // [1, 2, 3, 4, 5, 6]
arrFlatten([1, 2, 3]); // [1, 2, 3]
arrFlatten([]); // []
// With array-like objects
arrFlatten({ length: 2, 0: 1, 1: [2, 3] }); // [1, 2, 3]
The arrFlatten() method returns a new array with all sub-array elements flattened up to the specified depth (default 1).
Use this helper when the input already contains nested arrays and you only need to control how deeply to flatten. Flattening is depth-based and only applies to values that are arrays; non-array values are copied into the output unchanged.
For map-then-flatten workflows, use
arrFlatMap()which always flattens mapped results by exactly one level.