var arr1 = [1, 2, [3, 4, [5, 334]]]; Array.prototype.myFlat = function (newArr = []) { this.forEach((item) => { Array.isArray(item) ? item.myFlat(newArr) : newArr.push(item); }) return newArr } console.log(arr1.myFlat())