我来了across下面的一个函数,但不完全确定它在做什么。
.filter(note => note)
的具体用途是什么?
laneNotes: props.lane.notes.map(id => state.notes[
state.notes.findIndex(note => note.id === id)
]).filter(note => note)
filter
也会为每个 notes
执行一次,或者在所有 notes
被 map
循环后才执行一次?
最佳答案
.filter(note => note)
将过滤所有 falsy
值。它等同于:.filter(Boolean)
Also does filter get executed for each notes
or only once after all notes are looped over by map?
来自docs :
The filter() method creates a new array with all elements that pass the test implemented by the provided function
console.log([0, 2, '', false, null, true, undefined].filter(item => item));
console.log([0, 2, '', false, null, true, undefined].filter(Boolean));
全部falsy JavaScript 中的值:
null
错误
0
''
/""
未定义
NaN
关于javascript - Map、findIndex 和 filter 组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50205608/