const flatten = (arr) => { return arr.reduce((prev, curr) => { return Array.isArray(curr) ? prev.concat(flatten(curr)) : prev.concat(curr); }, []); } const arr1 = [1, [2, [3, 4]], 5]; const arr2 = flatten(arr1); console.log(arr2); // [1, 2, 3, 4, 5]