Javascript Object Keys

| | | | | |

👻 See our latest reviews to choose the best laptop for Machine Learning and Deep learning tasks!

The Object.keys () JavaScript method retrieves the keys in an object and returns a list that contains the keys. The order of the keys in the final list is the order in which they appear in the original object

JavaScript (JS) objects consist of two main parts: .. Keys and values

When working with an object, it may be necessary to retrieve a list of keys associated with it. This is where the JavaScript Object.keys () method comes in. This method allows you to generate a list of all the property names in an object. By using examples

This tutorial will explain how to use the Object.keys () method to return a list of keys stored in an object. We will also briefly discuss the structure of JavaScript

JavaScript objects. An Update

Objects (with an uppercase "O") are the data type of mapping in JavaScript. Key value card. The values ‚Äã‚Äãrecorded by an object can include strings, numbers, Booleans, and other data types. Here is an example of an object in JavaScript:

the words to the left of the colon (:) are the keys of our dictionary. The words to the right of the colon are the values. You can think of JavaScript objects as the equivalent of a Python dictionary, but in JavaScript.

JavaScript Object.keys ()

The JavaScript Object.keys () method returns all keys’ inside a JavaScript object or a JSON object. These keys are stored in the order they appear in the object

The syntax for the Object.keys () method is:.

The Object.keys () method takes one parameter: the name of the object whose keys you want to retrieve. This method returns the names of all keys for the specified object, stored as a JavaScript list

Note that the method itself is called Object.keys (). in effect, the () keys is an object method. you must specify the object whose keys you want to retrieve as a .keys () object parameter. It is not possible to add keys () to any object because that object will not have access to the keys () method.

JavaScript example Object.keys ()

We will walk you through an example to illustrate how this method works.

Previously, we created a dictionary called "job_description", which gives an overview of information about a job available at a local supermarket. The object contains a Boolean JavaScript , a string, and an integer. now suppose we want to retrieve a list of keys in this dictionary.

we could create this list using the following code:

 = const job_description { position: "Sales Assistant", floor_worker: true, hours_per_week: 38}; var job_keys = Object.keys (job_description); console.log (job_keys); 

Our code returns:

First , we declared a constant called" job_description ", which stores an object containing information about a job available at a local supermarket.

Next, we used Object.keys () to retrieve a list of keys associated with the dictionary and assigned the list to the "job_keys" variable. Next, we printed the contents of the "job_keys" list JavaScript console.

As you can see, our code returned a list that contains three values. Each value represents a unique key name in our object

JS Object.keys (): .. Another example

Let’s discuss another example of how to use this method

Suppose we want to print a list of the keys of our object, preceded by " Key name:". We could do it using this code:

Our code returns:

We first defined our "job_description" object whose keys we want to print to the console. Next, we used the JS Object.keys () method to retrieve a list of keys for our object

Next, I like to use a "for ... of" loop for iterate through each key in our "job_description" object

For each key, we printed " Key name:"., followed by the key name , to the console. If you are interested in learning more about how "to ..." loops work, read our Beginner’s Guide to JavaScript Loops for Loops .