object contains code example
Example 1: javascript does object have property
var person = {'first_name': 'bill','age':20};
if ( person.hasOwnProperty('first_name') ) {
}
Example 2: hasOwnProperty
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).
const object1 = {};
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1'));
console.log(object1.hasOwnProperty('toString'));
Example 3: js does object contain value
var obj = { a: 'test1', b: 'test2' };
if (Object.values(obj).indexOf('test1') > -1) {
console.log('Value exists!');
}
Example 4: javascript object includes
const person = {
first_name: "Sam",
last_name: "Bradley"
};
Object.values(person).includes("Bradley");
Example 5: object contains property javascript
if (x.hasOwnProperty('y')) {}
if ('y' in x) {}
if (x?.y){}
Example 6: check if field exists in object javascript
if ('field' in obj) {
}