Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Associative Array: Associative arrays are used to store key-value pairs. For example, to store the marks of the different subjects of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjects’ names as the keys in our associative array, and the value would be their respective marks gained. In an associative array, the key-value pairs are associated with a symbol. 

These are the following methods to get a list of Associative Array Keys:

Method 1: Using JavaScript foreach loop

In this method, traverse the entire associative array using a foreach loop and display the key elements of the array. 

Syntax:

for (let key in dictionary) {
// do something with key
}

Example: In this example, we will loop through the associative array and print keys of the array. 

javascript




// Script to Print the keys using loop
// Associative array
let arr = {
    Newton: "Gravity",
    Albert: "Energy",
    Edison: "Bulb",
    Tesla: "AC",
};
 
console.log("Keys are listed below");
 
// Loop to print keys
for (let key in arr) {
    if (arr.hasOwnProperty(key)) {
        // Printing Keys
        console.log(key);
    }
}


Output

Keys are listed below
Newton
Albert
Edison
Tesla

Method 2: Using Object.keys() method

The Object.keys() is an inbuilt function in javascript that can be used to get all the keys of the array. 

Syntax:

Object.keys(obj);

Example: Below program illustrates the use of Object.keys() to access the keys of the associative array. 

javascript




// Script to Print the keys
// using Object.keys() function
// Associative array
let arr = {
    Newton: "Gravity",
    Albert: "Energy",
    Edison: "Bulb",
    Tesla: "AC",
};
// Get the keys
let keys = Object.keys(arr);
 
console.log("Keys are listed below ");
 
// Printing keys
console.log(keys);


Output

Keys are listed below 
[ 'Newton', 'Albert', 'Edison', 'Tesla' ]

Method 3: Using Object.getOwnPropertyNames() method

The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties.

Syntax: 

Object.getOwnPropertyNames(obj)

Example: In this example, we will use JavaScript Object.getOwnPropertyNames() method to get all the keys of the Given Associative array.

Javascript




// Input Associative Array
let arr = {
    Newton: "Gravity",
    Albert: "Energy",
    Edison: "Bulb",
    Tesla: "AC",
};
 
// getting keys using getOwnPropertyNames
const keys = Object.getOwnPropertyNames(arr);
console.log("Keys are listed below ");
 
// Display output
console.log(keys);


Output

Keys are listed below 
[ 'Newton', 'Albert', 'Edison', 'Tesla' ]

Method 4: Using Object.entries() method

JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Syntax:

Object.entries(obj);

Example: In this example, we are using Object.entries() method.

Javascript




let associativeArray = { name: 'John', age: 25, city: 'New York' };
let keysArray = Object.entries(associativeArray).map(([key, _]) => key);
console.log("Keys are listed below ");
console.log(keysArray); // Output: ['name', 'age', 'city']


Output

Keys are listed below 
[ 'name', 'age', 'city' ]

Method 5: Using Reflect.ownKeys() method

JaScript Reflect.ownKeys() method in Javascript is used to return an array of the target object’s own property keys and it ignores the inherited properties.

Syntax:

Reflect.ownKeys( obj )

Example: In this example, we are using Reflect.ownKeys() method.

Javascript




let associativeArray = { name: 'John', age: 25, city: 'New York' };
let keysArray = Reflect.ownKeys(associativeArray);
console.log("Keys are listed below ");
console.log(keysArray); // Output: ['name', 'age', 'city']


Output

Keys are listed below 
[ 'name', 'age', 'city' ]


Last Updated : 03 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads