vue3如何判断一个json对象中是否包含指定的key
在Vue3中,你可以使用Object的hasOwnProperty方法来判断一个JSON对象中是否包含指定的key。
以下是一个示例代码:
const json = {
name: 'John',
age: 25,
gender: 'male'
};
const hasKey = Object.hasOwnProperty.call(json, 'name');
console.log(hasKey); // true
const hasAnotherKey = Object.hasOwnProperty.call(json, 'address');
console.log(hasAnotherKey); // false
在上面的代码中,我们使用Object.hasOwnProperty.call(json, key)来判断json对象中是否包含指定的key。如果包含,返回true;否则,返回false。
原文地址: http://www.cveoy.top/t/topic/hVNY 著作权归作者所有。请勿转载和采集!