确定javascript对象中所有属性是null还是空字符串的最优雅方法是什么?它适用于任意数量的属性。
{'a':null, 'b':''} //should return true for this object
{'a':1, 'b':''} //should return false for this object
{'a':0, 'b':1} //should return false
{'a':'', 'b':''} //should return true
创建一个函数来循环并检查:
function checkProperties(obj) {
for (var key in obj) {
if (obj[key] !== null && obj[key] != "")
return false;
}
return true;
}
var obj = {
x: null,
y: "",
z: 1
}
checkProperties(obj) //returns false
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句