How to get Keys, Values, and Entries in JavaScript Object?

Key takeaways

  • JavaScript object stores data as key-value pairs, allowing organized and flexible data management. To get keys, values, and entries, use the following methods:

    • Object.keys(obj): This retrieves an array of the object's keys.

    • Object.values(obj): This retrieves an array of the object's values.

    • Object.entries(obj): This retrieves an array of key-value pairs.

    • Reflect.ownKeys(obj): This retrieves an array of all keys, including both string and symbolic keys.

    • Object.getOwnPropertySymbols(obj): This retrieves an array of only symbolic keys of the object.

  • These methods improve the ability to efficiently manage, access, and manipulate JavaScript objects.

JavaScript by Brendan Eich provides a variety of data types to store various types of data. Among them, JavaScript objects are used to store data collections in the form of key-value pairs. To get the JavaScript keys, values, and entries of a JavaScript object data type, we use the following method:

These methods give developers different ways to work with objects in JavaScript, which facilitates data manipulation and other operations in applications.

Let’s say we have an object named user:

let user ={
name : "Arya",
age : 23,
greet : () => "Hello",
};

Retrieving the keys of the user object

We use the Object.keys() method to retrieve the keys of the user object:

let user ={
name : "Arya",
age : 23,
greet : () => "Hello",
};
let keys = Object.keys(user);
console.log(keys); //["name", "age", "greet"]

Object.keys() returns the keys that are available in the object while calling the function—any new keys added after this are not updated.

let keys = Object.keys(user);
keys; //["name", "age", "greet"]
user.weight = 65;
// weight key will not be added to keys array
console.log(keys); //["name", "age", "greet"]

Getting the values of the user object

We use the Object.values() method to get the values of the user object:

let values = Object.values(user);
console.log(values); // ["Arya", 23, ƒ, 65]

Getting the entries of the user object

We use the Object.entries() method to get the key-value pairs of the user object:

let entries = Object.entries(user);
console.log(entries);

We can use the Object.entries() to get the user object’s key-value pairs, and we can use array methods like map(), filter(), and reduce() on the object.

Using map() method with Object.entries()

In the following code, we process each pair using the map() method, which iterates over each entry of user, destructures each entry into key and val, and transforms them into a formatted string like "The ${key} is ${val}". This iteration results in an array of descriptive strings, which is then logged to the console, displaying "The name is Arya" and "The age is 23".

let user ={
name : "Arya",
age : 23,
};
let entries = Object.entries(user)
let data = entries.map( ([key, val] = entry) => {
return `The ${key} is ${val}`;
});
console.log(data); //  ["The name is Arya", "The age is 23"]

Note: When using the Object.keys() method, symbolic keys are not returned. Instead, we use the Reflect.ownKeys(obj) method call to return all keys. Or, we use Object.getOwnPropertySymbols to get only the symbolic keys.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How do we perform JavaScript object inspection?

We can perform object inspection using the console.log() command. It prints the object’s properties and values to the console.


How do we get a particular key-value from an object in JavaScript?

To access a specific key-value pair in a JavaScript object, we can use Object.entries() to convert a JavaScript object into an array of key-value pairs and then filter this array to retrieve the value associated with a specific key.

let obj = { name: 'Arya', age: 30 };
let key = 'name';

// Convert the object to an array of key-value pairs
let entries = Object.entries(obj);

// Find the key-value pair where the key matches the specified key
let keypair = entries.find(([k, v]) => k === key);

console.log(keypair); 

What is the difference between object keys and object entries?

Object keys represent the keys in an object’s key-value pairs, which can be retrieved using Object.keys(obj), while object entries represent each key-value pair in the object, which can be obtained using Object.entries(obj).


How do we get a number of keys in an object in JavaScript?

We can retrieve a number of keys in the object using the length property of the array returned by Object.keys().

let user = {
  name: "Arya",
  age: 30,
};

let numKeys = Object.keys(user).length;
console.log(numKeys); // Output: 2

How do we get a unique array of objects in JavaScript?

We can get a unique array of objects in JavaScript by using any one of the methods: