pro in obj is enough;
This method can determine whether the object's own properties and inherited properties exist.
Properties in object:
let infoObj = {
name: "六卿",
age: 18,
habby: ['唱歌', '贪财好色'],
friends: {
'张三': 'Y',
'小李': 'N'
}
}
console.log('name' in infoObj, 'name')
console.log('age' in infoObj, 'age')
console.log('habby' in infoObj, 'habby')
console.log('friends' in infoObj, 'friends')
console.log('六卿' in infoObj, '六卿')
console.log('张三' in infoObj.friends, '张三')
console.log('小李' in infoObj.friends, '小李')
console.log('六卿' in infoObj.friends, '六卿')
You can also use the hasOwnProperty() method that comes with the object to judge
. Usage: object.hasOwnProperty(pro) returns a boolean value
Note: Only the senior attributes of the object can be judged, and the inherited attributes cannot be judged
You can also use or directly use if judgment or trinocular judgment