Пытался объяснить с комментариями, надеюсь, это поможет.
const colors = ['red', 'red', 'green', 'blue', 'green', 'yellow'];
console.log(colors);
const distinctColors = colors.reduce(
(distinct, color) =>
(distinct.indexOf(color) !== -1) ?
// ----------------^ Turnary to test for presence of current color in the accum []
distinct :
// ----^ It DOES exist, so return the current Accum array
[...distinct, color], []
// ---^ Is DOES NOT exist, return a new array of Accum + Color
// --------------------^ This initialises a new empty array into the accumulator
)
console.log(distinctColors)
Только что добавил это для справки, использование набора для этого гораздо эффективнее.
const colors = ['red', 'red', 'green', 'blue', 'green', 'yellow'];
console.log(colors);
const distinctColors = [...new Set(colors)];
console.log(distinctColors)
Вот документация MDN по Set. Набор Javascript