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