所以第一个对象的值具有属性" 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");
答案 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