Day 259/300 How to determine whether an object contains a certain property?

(1) Demand

Want to determine whether an object contains attribute values.

(2) Ideas

1. It is possible to traverse all the properties of the object and make loop judgments

2. You can directly write obj[key]

3. Use the Object.hasOwn method

(3) Demo

const object1 = {
    
    
  prop: 'exists'
};

console.log(Object.hasOwn(object1, 'prop'));
// expected output: true

console.log(Object.hasOwn(object1, 'toString'));
// expected output: false

console.log(Object.hasOwn(object1, 'undeclaredPropertyValue'));
// expected output: false

Guess you like

Origin blog.csdn.net/xinghuowuzhao/article/details/126936918