every()方法没有返回正确的值

时间:2016-09-27 02:24:34

标签: javascript algorithm boolean

有人可以告诉我为什么这会回归真实吗?它应该返回false,因为在第一次迭代中值[pre] === 0 ...谢谢。

function truthCheck(collection, pre) { 
   function check(value){
      if(value.hasOwnProperty(pre)){
         return value[pre] !== null || value[pre] !== undefined || value[pre] !== ""|| value[pre] !== 0; 
      }
   } 
   return collection.every(check);
}    
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age");

1 个答案:

答案 0 :(得分:1)

使用&&运算符

return (value[pre] !== null 
        && value[pre] !== undefined 
        && value[pre] !== "" 
        && value[pre] !== 0);

jsfiddle https://jsfiddle.net/4wcovask/