js 判断对象(字典)是否有某个属性, 判断对象某个属性是否有值

1 判断是否有某个属性

const obj = { name: 'John', age: 30 };

if ('name' in obj) {
  console.log('obj has name property');
}

2 判断某个属性是否有值

const obj = { name: 'John', age: 30 };

if (typeof obj.name !== 'undefined') {
  console.log('obj.name has a value');
}

你可能感兴趣的:(js,javascript,前端,vue.js)