我在理解此reduce示例的if语句时遇到问题:
const colors = ['red', 'red', 'green', 'blue', 'green', 'yellow'];
console.log(colors);
const distinctColors = colors.reduce(
(distinct, color) =>
(distinct.indexOf(color) !== -1) ?
distinct :
[...distinct, color], []
)
console.log(distinctColors)
我试图理解伪代码中的if语句,并阅读此示例,但始终看到如下内容:
If the color found in the distinct array (which is empty)
return empty array
else
return contents of array and color added to an empty array
我要关闭还是要离开?
繁星点点滴滴
慕沐林林
相关分类