function flattening(arr = []) { let newArr = []; arr.forEach(item => { if (Array.isArray(item)) { newArr.push(...flattening(item)); } else { newArr.push(item); } }); return [...new Set(newArr)]; } let a = [1, 'ok', 'ok', 2, [3, 4], [[[[[5, 6], 7]], 8], 1, 2, 3, 3]]; console.log(flattening(a)); // => [1, "ok", 2, 3, 4, 5, 6, 7, 8]