javascript .filter()真正的布尔值 [英] javascript .filter() true booleans
本文介绍了javascript .filter()真正的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function bouncer(arr) {
// Don't show a false ID to this bouncer.
function a(b) {
if(b !== false) {
return b;
}
}
arr = arr.filter(a);
return arr;
}
bouncer([7, 'ate', '', false, 9]);
我只需返回真正的布尔语句,当我运行此代码时,它可以工作。但是,我很困惑,因为我的if语句将起作用,无论是b!== true还是b!== false。有人可以解释为什么这两种方式都有效吗?
I have to return true boolean statements only, and when I run this code, it works. However, I am quite confused because my "if statement" will work whether it's b !== true or b !== false. Could someone please explain the reason why this works both ways?
推荐答案
我正在解决类似的问题并想出了这个:
I was solving a similar problem and came up with this:
function bouncer(arr) {
return arr.filter(Boolean);
}
bouncer([7, 'ate', '', false, 9]);
// returns ['7','ate','9']
这篇关于javascript .filter()真正的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文