To flatten an array using built-in methods.
function flatten(arr) { const result = []/*from www . ja v a2 s . c om*/ arr.forEach((i) => { if (Array.isArray(i)) { result.push(...flatten(i)) } else { result.push(i) } }) return result } // Usage const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]] const a = flatten(nested); console.log(a);