javascript .filter() true 布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30016773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:29:54  来源:igfitidea点击:

javascript .filter() true booleans

javascriptfilterboolean

提问by jyoon006

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]);

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?

我只需要返回真正的布尔语句,当我运行这段代码时,它就可以工作了。但是,我很困惑,因为无论是 b !== true 还是 b !== false,我的“if 语句”都有效。有人可以解释为什么这两种方式都有效吗?

回答by hello world

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']

回答by leonel.ai

Apparently .filter()was introduced in ES5.

显然.filter()是在 ES5 中引入的。

This definitely helped me wrap my mind around what's going on here. Hope it helps!

这绝对帮助我了解这里发生的事情。希望能帮助到你!

Essentially, writing:

本质上,写作:

arr.filter(Boolean)

is the same as writing:

和写一样:

arr.filter( function(x) { return Boolean(x); }); 

since Boolean()is also a function that returns truthy when true and falsy when false!

因为Boolean()也是一个函数,它在真时返回真,假时返回假!

Example:

例子:

var a = [1, 2, "b", 0, {}, "", NaN, 3, undefined, null, 5];

var b = a.filter(Boolean);  // [1, 2, "b", {}, 3, 5]; 

Source: here.

来源:这里

回答by mahig

simplest way to do it :

最简单的方法:

function bouncer(arr) {
    return arr.filter(x => !!x);
}

回答by Emil Ingerslev

It's because you return the value. The filter function should return true or false like this:

这是因为您返回了值。过滤器函数应该像这样返回真或假:

function bouncer(arr) {
    arr = arr.filter(function(x) { console.log(x === true)
       if(x !== false) {
           return true;
       }
    });
    return arr;
}

or shorter:

或更短:

function bouncer(arr) {
    return arr.filter(function(x) { console.log(x === true)
       return x !== false;
    });
}

回答by jdphenix

Your function acts is it does because JavaScript values in boolean comparisons are "truthy" or "falsey". Non-booleans are coerced to a boolean value when used in a boolean context (comparisons, if statements, etc.)

您的函数会起作用,因为布尔比较中的 JavaScript 值是“真”或“假”。在布尔上下文(比较、if 语句等)中使用时,非布尔值被强制为布尔值

If I understand your intent, you can modify your function to get your expected output like this:

如果我理解您的意图,您可以修改您的函数以获得您的预期输出,如下所示:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
    function a(b) {
      if(typeof(b) === 'boolean' && !b) {
        return new Boolean(b);
      }
    }

    arr = arr.filter(a);
    return arr;
}

bouncer([7, 'ate', '', false, 9, true]);

回答by Dr Gaud

I came across this exercise and was playing about with it somewhat. It ended up helping me understand it a bit better, so if you may I will add a bit of clarity to this bouncer function.

我遇到了这个练习,并在某种程度上玩弄它。它最终帮助我更好地理解了它,所以如果你可以的话,我会为这个保镖功能添加一些清晰度。

function bouncerOne(array){

let truthy = array.filter(x => x);
// This takes the .filter() and applies the anon function where it takes 
// the value and returns the positive value. All values that are not Falsy including 
// objects and functions would return True. 
console.log("The Truth is:", truthy);//this Outputs the result to the console.
let falsy = array.filter(x => !x); 
//This takes the .filer() and applies the 
//annon function where it takes the value and only returns items that ARE NOT True. 
//So Falsy Values are taken. 
console.log("The Falsy Values are",falsy);

//The annon function of (x => x) & (x => !x)
// really makes use of the strict equality power of JavaScript. 
// Where simple conditional test can be carried out.
};
console.log(bouncerOne([7, "ate", "", false, 9,null,42,"Bingo",undefined,NaN]));
//Returns: The Truth is: [ 7, 'ate', 9, 42, 'Bingo' ]
//The Falsy Values are [ '', false, null, undefined, NaN ]