How to Get a List of Array Keys in JavaScript?
Here are the different methods to get a list of associative array keys in JavaScript
1. Using JavaScript for each loop
In this method, traverse the entire associative array using a for each loop and display the key elements of the array.
//Driver Code Starts
let a = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"};
//Driver Code Ends
for (let key in a) {
if (a.hasOwnProperty(key)) {
console.log(key);
}
}
Output
Newton Albert Edison Tesla
In this example
- It checks if each key belongs directly to arr (not inherited) with hasOwnProperty().
- Valid keys (Newton, Albert, Edison, Tesla) are logged to the console.
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.
let arr = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"};
let keys = Object.keys(arr);
console.log(keys);
Output
[ 'Newton', 'Albert', 'Edison', 'Tesla' ]
In this example
- The code retrieves the keys of the associative array arr using Object.keys(), which returns an array of the keys.
- It then logs the keys to the console.
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.
let a = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"};
const keys = Object.getOwnPropertyNames(a);
console.log(keys);
Output
[ 'Newton', 'Albert', 'Edison', 'Tesla' ]
In this example
- The code retrieves and prints the keys (property names) of the arr object using Object.getOwnPropertyNames().
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.
let a = { name: 'Jaya', age: 25, city: 'New York' };
let keys = Object.entries(a).map(([key, _]) => key);
console.log(keys);
Output
[ 'name', 'age', 'city' ]
In this example
- The code retrieves and prints the keys of the a object by using Object.entries() and map().
- It converts the object's entries into an array of keys and then logs them.
5. Using Reflect.ownKeys() method
JavaScript 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.
let a = { name: 'Jaya', age: 25, city: 'New York' };
let keys = Reflect.ownKeys(a);
console.log(keys);
Output
Keys are listed below [ 'name', 'age', 'city' ]
In this example
- The code uses Reflect.ownKeys() to retrieve all the keys (including non-enumerable ones) of the a object and logs them.
6. Using Object.getOwnPropertySymbols() method
In JavaScript, the Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.
let s1 = Symbol('a');
let s2 = Symbol.for('b');
let obj = {
[s1]: 'hello',
[s2]: 'world'
};
let symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols);
Output
[ Symbol(a), Symbol(b) ]
In this example
- The code retrieves and logs the symbol keys of the obj object using Object.getOwnPropertySymbols().