Get an Object's Key by its Value using JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Get an Object's Key by its Value in JavaScript

To get an object's key by its value:

  1. Call the Object.keys() method to get an array of the object's keys.
  2. Use the find() method to find the key that corresponds to the value.
  3. The find method will return the first key that meets the condition.
index.js
function getObjectKey(obj, value) { return Object.keys(obj).find(key => obj[key] === value); } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Carl', }; console.log(getObjectKey(obj, 'Alice')); // ๐Ÿ‘‰๏ธ "employee1"

get object key by its value

The code for this article is available on GitHub

We used the Object.keys() method to get an array containing the object's keys.

index.js
const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Carl', }; // ๐Ÿ‘‡๏ธ [ 'employee1', 'employee2', 'employee3' ] console.log(Object.keys(obj));

We then called the Array.find() method on the array of keys.

The function we passed to the find() method gets called with each element (key) in the array until it returns a truthy value or iterates over the entire array.
index.js
function getObjectKey(obj, value) { return Object.keys(obj).find(key => obj[key] === value); }

On each iteration, we use the key to access the object's value and compare it to the supplied value.

If the equality comparison evaluates to true, the find() method returns the corresponding key and short-circuits.

If the equality check never returns true, the find method returns undefined.

index.js
function getObjectKey(obj, value) { return Object.keys(obj).find(key => obj[key] === value); } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Carl', }; console.log(getObjectKey('Dean')); // ๐Ÿ‘‰๏ธ undefined

# Array.find() vs Array.filter()

The Array.find() method returns the first element for which the testing function returns a truthy value.

If you have multiple keys with the same value, you will get the name of the first key.

index.js
function getObjectKey(obj, value) { return Object.keys(obj).find(key => obj[key] === value); } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Alice', }; console.log(getObjectKey(obj, 'Alice')); // ๐Ÿ‘‰๏ธ employee1

using array find vs array filter

The code for this article is available on GitHub

To get multiple object keys that store the same value:

  1. Call the Object.keys() method to get an array of the object's keys.
  2. Call the Array.filter() method on the array of keys.
  3. The filter() method will return a new array containing all the matching keys.
index.js
function getObjectKeys(obj, value) { return Object.keys(obj).filter(key => obj[key] === value); } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Alice', }; // ๐Ÿ‘‡๏ธ [ 'employee1', 'employee3' ] console.log(getObjectKeys(obj, 'Alice'));

By using the Array.filter() method we get an array of all the keys that satisfy the condition.

The function we passed to the Array.filter() method gets called with each element in the array.

The filter() method returns a new array that only contains the elements that meet the condition.

Alternatively, you can use a simple for...of loop.

# Get an Object's Key by its Value using a for...of loop

This is a three-step process:

  1. Use a for...of loop to iterate over the object's keys.
  2. Check if the current value is equal to the supplied value.
  3. If the condition is met, return the corresponding key.
index.js
function getObjectKey(obj, value) { for (const key of Object.keys(obj)) { if (obj[key] === value) { return key; } } } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Carl', }; console.log(getObjectKey(obj, 'Alice')); // ๐Ÿ‘‰๏ธ employee1

get object key by its value using for of loop

The code for this article is available on GitHub

We used a for...of loop to iterate over the object's keys.

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the current value is equal to the supplied value.

If the condition is met, we return the corresponding key and exit the iteration.

The code sample returns the first key in the object that stores the supplied value.

If you need to get all of the keys with the value, push the matching keys into an array.

# Get all of an Object's keys that store a supplied value using for..of

This is a four-step process:

  1. Declare a new variable and initialize it to an empty array.
  2. Use a for...of loop to iterate over the Object's keys.
  3. Check if the current value is equal to the supplied value.
  4. If the condition is met, push the current key into the new array.
index.js
function getObjectKeys(obj, value) { let keys = []; for (const key of Object.keys(obj)) { if (obj[key] === value) { keys.push(key); } } return keys; } const obj = { employee1: 'Alice', employee2: 'Bobby', employee3: 'Alice', }; // ๐Ÿ‘‡๏ธ [ 'employee1', 'employee3' ] console.log(getObjectKeys(obj, 'Alice'));
The code for this article is available on GitHub

We initialized the keys variable to an empty array and used the for...of loop to iterate over the object's keys.

On each iteration, we check if the current value is equal to the supplied value.

If the condition is met, we use the Array.push() method to add the current key to the keys array.

The last step is to return the keys array from the function.

Which approach you pick is a matter of personal preference. I'd use the Array.find() or Array.filter() methods as I find them more direct and just as intuitive.

I've also written an article on how to get an object's key or value by index.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev