[ 1, 2, 3, 4, 5].reduce((sum, el, _, arr) => {
if (el === 4) {
arr.length = 0;//Array passed to callback is now empty
return sum;
}
return sum + el;
}); // return 1 + 2 + 3 = 6
const array = ['apple', '-pen', '-pineapple', '-pen'];
const x = array
.slice(0) // create copy of "array" for iterating
.reduce((acc, curr, i, arr) => {
if (i === 2) arr.splice(1); // eject early by mutating iterated copy
return (acc += curr);
}, '');
console.log("x: ", x, "
original Arr: ", array);
// x: apple-pen-pineapple
// original Arr: ['apple', '-pen', '-pineapple', '-pen']