是否有 Javascript/Lodash 函数来检查对象是否缺少任何属性?

问题描述 投票:0回答:1

我想检查一个 javascript 对象以查看它的任何属性是否丢失,而不单独检查每个属性。

我可以使用“hasownproperty”或执行 if/else 语句来检查每个语句是否为空/未定义,我想要一种更短的方法来检查对象。

javascript object properties lodash
1个回答
0
投票

也许是这个?

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))

© www.soinside.com 2019 - 2024. All rights reserved.