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