我想检查一个 javascript 对象以查看它的任何属性是否丢失,而不单独检查每个属性。
我可以使用“hasownproperty”或执行 if/else 语句来检查每个语句是否为空/未定义,我想要一种更短的方法来检查对象。
也许是这个?
const hasMissing = obj => {
const vals = Object.values(obj);
return vals.filter(val => val !== null && val !== undefined).length !== vals.length
};
const obj = { "a":null, "b":"there", "c":0, "d":undefined }
console.log(obj);
console.log(JSON.stringify(obj)); // so we cannot use that
console.log("Missing?",hasMissing(obj))