const flatten = (arr, depth) => { if (!Array.isArray(arr) || depth <= 0) return arr return arr.reduce((acc, cur) =>{ if (Array.isArray(cur)) { return acc.concat(flatten(cur, depth -1)) } else { return acc.concat(cur) } }, [])}