Object.hasOwnPropertyDeep()
const Person = {
name: "Bob",
address: {
city: "NoCity",
pincode: 123456,
},
};
Object.prototype.hasOwnPropertyDeepPolyfill = function (target) {
function hasProperty(OBJ) {
if (OBJ === null) return false;
for (let key in OBJ) {
if (key === target) return true;
if (OBJ[key].constructor.name === "Object") {
return hasProperty(OBJ[key]);
}
}
return false;
}
return hasProperty(this);
};
console.log(Person.hasOwnPropertyDeepPolyfill("pincode")); // true
console.log(Person.hasOwnPropertyDeepPolyfill("arjun")); // false