How to get a list of associative array keys in JavaScript?

Associative Array: An associative array is simply a set of key value pairs. The value is stored in association with its key and if you provide the key the array will return the value. This is all an associative array is and the name comes from the association between the key and the value. For example to store the data of customers in an array, a numerically indexed array would not be a good practice and easy to handle.

Now the question is how to get a list of associative array keys? We can get the keys of associative array using Object.keys() function. Object.keys() is an inbuilt function of JavaScript to get all the keys of an array.

Below code snippet explains the use of Object.keys() to accessing the keys of associative array.

Output: [“firstName”, “lastName”, “job”]

Same result can be achieved by another approach. By iterating the array as shown below:

Output: [“firstName”, “lastName”, “job”]

So we got the same result as compared to first approach.

Leave a Reply

Your email address will not be published. Required fields are marked *