Flattening an array means removing its nested array structure while keeping the values in their original order. For example, [1, [2, [3]]] becomes [1, 2, 3].
This is useful when combining grouped records or collecting results produced in batches. The arrays may be nested at different depths, and their values can include numbers, strings, objects, or other data. Flattening should preserve those values while producing one sequence that callers can iterate over.
Implement a function flatten that returns a newly created array with all subarray elements concatenated recursively into a single level.
// Single-level arrays are unaffected.flatten([1, 2, 3]); // [1, 2, 3]// Inner arrays are flattened into a single level.flatten([1, [2, 3]]); // [1, 2, 3]flatten([[1, 2],[3, 4],]); // [1, 2, 3, 4]// Flattens recursively.flatten([1, [2, [3, [4, [5]]]]]); // [1, 2, 3, 4, 5]