.filter()在javascript / typescript中(.filter() in javascript/typescript)

编程入门 行业动态 更新时间:2024-10-27 21:11:23
.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))

更多推荐

本文发布于:2023-07-31 01:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:filter   javascript   typescript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!