Judgment method: 1. Use the "Object.keys(obj)" statement to return an array containing all the keys of the obj object; 2. Use the "array.includes("key value")" statement to determine the key array Whether the specified value exists in the object. If the return value is true, the specified key exists in the object, otherwise it does not exist.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 Determine whether the object key exists
Implementation idea:
Use Object The .keys() method obtains all the keys (key names) of the object, and the return value is an array
Use the includes() method to determine whether a key array contains a specified key value
Implementation code: Determine whether the key name "a" exists in the object obj
var obj = { a:"小白", b:"小黑", c:"大黄" }; var keys=Object.keys(obj); console.log(keys); if(keys.includes("a")){ console.log("obj对象中存在指key"); }else{ console.log("obj对象中不存在指key"); }
[Related recommendations: javascript Video tutorial、web front-end】
The above is the detailed content of How to determine whether the object key exists in es6. For more information, please follow other related articles on the PHP Chinese website!