简体   繁体   English

为什么我使用布尔对象变得不确定?

[英]Why i'm getting undefined using Boolean object?

function boo(arr)
{
for(var i=0;i<arr.length;i++)
 { 
   var bob = new Boolean(arr[i]);
   if(bob === false)
    {

     console.log(arr[i]);   

    }

 }
}
console.log(boo([0,"how",89,false]));

I want to check all the false values like false,null,undefined etc.from an array using Boolean Object and print it. 我想使用布尔对象从数组中检查所有错误值,例如false,null,undefined等,并将其打印出来。 How can i do it using Boolean Object?. 我该如何使用布尔对象? How can i add multiple values for checking whether it is true or false in Boolean Object? 如何在布尔对象中添加多个值以检查它是true还是false

You can't compare Boolean objects that way in javascript. 您无法在javascript中以这种方式比较Boolean对象。 new Boolean(...) === false will always evaluate to false . new Boolean(...) === false将始终评估为false

MDN has a good warning on the matter: MDN对此事提出了很好的警告

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. 不要将原始布尔值true和false与布尔对象的true和false值混淆。

For instance (given value = arr[i] ), if you need to see if something is truthy just use !!value . 例如(给定value = arr[i] ),如果您需要查看某件事是否属实,请使用!!value For falsey, use !value . 对于false,请使用!value If you need to see if something is really true use value === true , likewise for false , but not a Boolean instance. 如果需要查看某项是否确实为true使用value === true ,同样对于false ,也不要使用Boolean实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用jQuery获取[object object]? - Why i'm getting [object object] using jQuery? 为什么我得到的var undefined - Why I'm getting the var undefined 为什么我将数组值设为“未定义” - why I'm getting array values as “undefined” 为什么我得到未定义的对象,以及如何使用NodeJ解析json数据? - why I'm getting undefined object and How do I parse json data with NodeJs? 我在Ionic中得到一个未定义的变量,我不确定为什么吗? - I'mg getting an undefined variable in Ionic and I'm not sure why? 我正在尝试使用 JavaScript 将海报 URL 添加到我的 neo4j 电影数据库,但我一直收到此未定义对象错误 - I'm trying to add poster URLs to my neo4j movie database using JavaScript, but I'm getting this undefined object error all the time 为什么在进行发布请求时我得到空 object - Why I'm getting empty object when doing post request 我不确定为什么我在使用React Redux时看到未定义 - I'm unsure as to why i'm seeing undefined while using react redux 为什么我得到“undefined不是一个对象(评估PropTypes.shape)”? - Why am I getting “undefined is not an object (evaluating PropTypes.shape)”? 为什么我变得未定义不是Vue JS中的对象错误 - Why am I getting undefined is not an object error in vue js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM