.filter()在javascript / typescript中(.filter() in javascript/typescript)
.filter(
(item) => {
console.log(this.timeFilter); // line 1
if(this.timeFilter == NaN ) { return true; } // line 2
console.log("why log this?"); // line 3
return item.endTime < this.timeFilter;
}
)
我的代码是上面的,我有一个问题,即使this.timeFilter == NaN,它不会去第2行,但实际上去第3行,不太确定发生了什么
NaN // line 1 output why log this? // line 3 output happen .filter( (item) => { console.log(this.timeFilter); // line 1 if(this.timeFilter == NaN ) { return true; } // line 2 console.log("why log this?"); // line 3 return item.endTime < this.timeFilter; } )my code is above, I have a question that even if the this.timeFilter == NaN, it does not go the line 2, but go line 3 actually, not so sure what happened
NaN // line 1 output why log this? // line 3 output happen最满意答案
NaN == NaN在javascript中返回false 。 这是为了防止像这样的操作
'somename' / 5 == 'someothername' / 5; // should not return true (both are NaN)使用isNaN :
if(this.timeFilter == NaN)实际应该是if(!isNaN(this.timeFilter))
NaN == NaN returns false in javascript. This is to prevent operations like
'somename' / 5 == 'someothername' / 5; // should not return true (both are NaN)Use isNaN:
if(this.timeFilter == NaN) should be actually if(!isNaN(this.timeFilter))
更多推荐
发布评论