JS |为什么Object Truthy的属性虽然它是空的?

时间:2015-11-27 23:33:49

标签: javascript function object properties

所以第一个对象的值具有属性" single"是空的,但仍然真实,我错了什么?

 function every(collection, pre) {
  var rtr = null;

  for(var e in collection){
     if(collection[e][pre] !== null && 
        collection[e][pre] !== undefined && 
        collection[e][pre] !== 0 && 
        collection[e][pre] !== "" && 
        collection[e][pre] !== false && 
        collection[e][pre] !== NaN){

       rtr = true;
     }
     else 
        rtr = false;
  }

  console.log(rtr);

}

every([{"single": ""}, {"single": "double"}], "single");

1 个答案:

答案 0 :(得分:1)

你在循环之外console.log。试试这个(我也删除了流浪的`):

function every(collection, pre) {
  var rtr = null;

  for(var e in collection){
     if(collection[e][pre] !== null && 
        collection[e][pre] !== undefined && 
        collection[e][pre] !== 0 && 
        collection[e][pre] !== "" && 
        collection[e][pre] !== false && 
        collection[e][pre] !== NaN){

       rtr = true;
     }
     else 
        rtr = false;

     console.log(rtr);
  }

}

every([{"single": ""}, {"single": "double"}], "single");

记录

false
true