early break reduce()
codepen ⟩ early break reduce()?
const {log} = console;
// 0 1 2 3
const array = ['apple', '-pen', '-pineapple', '-pen'];
// early break reduce()
const x = array
.slice(0) // ⭐️ copy of "array"
.reduce((acc, curr, i, arr) => {
if (i === 2) arr.splice(i+1); // ⭐️ eject early by mutating `arr`
return (acc += curr); // (array copy)
}, '');
log(x); // "apple-pen-pineapple"
Last updated