Get the App
SLTechnology News&Howtos  ›  Development  › 

How does es6 determine whether an object key exists

Shulou Source: shulou.com Published: 2022-06-01 03:19:52 09月16日 Update

This article mainly introduces how es6 judges whether the object key exists or not, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

Judgment method: 1. Use "Object.keys(obj)" statement to return an array containing all keys of obj object;2. Use "Array.includes("key value ")" statement to judge whether the specified value exists in the key array. If the return value is true, the specified key exists in the object, otherwise it does not exist.

Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 Determine whether the object key exists

Implementation ideas:

Use the Object.keys() method to get all keys of the object. 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 key "a" exists in object obj

var obj = { a:"little white." b:"black"; c:"big yellow"};var keys=Object.keys(obj);console.log(keys);if(keys.includes("a")){ console.log("key exists in obj object");}else{ console.log("key does not exist in obj object");}

Thank you for reading this article carefully. I hope that the article "es6 how to judge whether the object key exists" shared by Xiaobian will be helpful to everyone. At the same time, I hope that everyone will support you more, pay attention to the industry information channel, and more relevant knowledge is waiting for you to learn!

Tags: Objects arrays articles methods statements code values interests at the same time rhubarb ideas tutorials more friends environment computers knowledge systems editing industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Shulou Technology Shulou Tech Info Microsoft