Open jsartisan opened 3 weeks ago
index.js
export function flatten(arr, depth = 1) { const result = []; for (let i = 0; i < arr.length; i++) { const current = arr[i]; if (Array.isArray(current) && depth > 0) { result.push(...flatten(current, depth - 1)); } else { result.push(current); } } return result; }
Sharing another solution that uses classic for loop instead of reduce.
index.js