场景需求:在我们调接口时,有时后端需要我们把某个值改成固定的格式传给他们,该值没有对应的value时,就不传给他们。
判定对象中某个属性是否存在:
方法一:
if (newObj.hasOwnProperty('reqLabelIds')) {
console.log('该属性存在,可以进行操作');
newObj.reqLabelIds = newObj.reqLabelIds.split(',');
} else {
console.log('该属性不存在');
}
方法二:
if (newObj.reqLabelIds !== undefined) {
console.log('该属性存在,可以进行操作');
newObj.reqLabelIds = newObj.reqLabelIds.split(',');
} else {
console.log('该属性不存在');
}
方法三:
if (newObj.reqLabelIds ) {
console.log('该属性存在,可以进行操作');
newObj.reqLabelIds = newObj.reqLabelIds.split(',');
} else {
console.log('该属性不存在');
}