person = {
id: 1,
name: 'John',
age: 46,
active: true
};
house = {
color: 'blue with white patterns',
years: 20,
isInGoodCondition: true
};
const myArray = [person, house];
myArray.forEach((element) => {
if (element?.name) console.log('It is a person object');
if (element?.isInGoodCondition) console.log('It is a house object');
});
1条答案
按热度按时间xcitsw881#
如果你想知道一个物体是否属于某种类型,我相信你可以这样问:
这个
object?.property
安全地问是否property
存在于对象内部。