我有一个具有键-值对的数据对象.数据对象包括4个具有相应值的属性.然而,从接口中拉取数据时,有时会显示2到3个属性,如下图所示(如果某个属性的数据不可用,则不会显示在对象中).我想将缺少的属性重新添加到同一个对象中,如下所示.
Data Output
var obj = {
property1: 100
property2: 80
property3: 50
}
我想在物体if property4 exists内进行判断.如果不是,则可以将其添加到布尔值为False的同一对象中
My code that checks all properties whether they are missing, but it is not working as expected个
var obj = {
property1: 100,
property2: 80,
property3: 50
}
;
const property_1 = obj.hasOwnProperty('property1');
console.log(property_1);
const property_2 = obj.hasOwnProperty('property2');
console.log(property_2);
const property_3 = obj.hasOwnProperty('property3');
console.log(property_3);
const property_4 = obj.hasOwnProperty('property4');
console.log(property_4);
if (property_1 === false || property_2 === false || property_3 === false || property_4 === false) {
obj.property1 = false
obj.property2 = false
obj.property3 = false
obj.property4 = false
}
console.log(obj)
//Console Output
{
property1: false,
property2: false,
property3: false,
property4: false
}
My Expected Result is:
{
property1: 100,
property2: 80,
property3: 50,
property4: false // Does not exist, hence it is false
}
我可以知道我错在哪里吗?