Home >Web Front-end >Front-end Q&A >How to determine whether the object key exists in es6

How to determine whether the object key exists in es6

青灯夜游
青灯夜游Original
2022-03-23 16:15:255750browse

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.

How to determine whether the object key exists in es6

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");
}

How to determine whether the object key exists in es6

[Related recommendations: javascript Video tutorialweb 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn