I am considering this reduction function in JavaScript.,
var colors = ['red', 'red', 'green', 'blue', 'green'];
var distinctColors = colors.reduce(
(distinct, color) =>
(distinct.indexOf(color) != -1) ?
distinct :
[...distinct, color],
[]
)
I understand that the callback function is called once for each element of the array colors, it searches for a string colorin distinctand simply returns the array if it is found, and adds colorin distinctif not found.
I do not understand how function parameters are (distict, color)defined as an empty array and each color.
Is JavaScript automatically assumed to be distinctan array because I am calling distinct.indexOf(color)??
source
share