How to get an Object’s Key by its Value using JavaScript

Get an Object’s Key by its Value in JavaScript

To get an object’s key by it’s value

  1. Call the Object.keys() method.
  2. Use the find() method to find the key that goes with a particular value.
  3. The find() method returns the first element in the provided array that passes the provided test function.

Example –

function getObjectKey(obj, value) {
  return Object.keys(obj).find(key => obj[key] === value);
}

const obj = {country: 'England', city: 'London'};

console.log(getObjectKey(obj, 'London'));
Get an Object's Key by its Value.
Get an Object’s Key by its Value.

We used the Object.keys method to get an array of the object’s keys.
Here’s what the above class is doing:

  1. We create a function called getObjectKey that takes two parameters: obj and value.
  2. We return the key of the object that matches the value.
  3. We create an object called obj.
  4. We log the key of the object that matches the value ‘London’.

Object.keys(obj) returns an array of the keys in the object.
find(key => obj[key] === value) returns the first element in the array that satisfies the condition.

ℹ️ The find() method calls the function we passed to it with each element (key) in the array, until the function returns a truthy value or it reaches the end of the array.

The find() method executes the function once for each element present in the array.

We compare the supplied value to the object’s value on each iteration, using the key to access the object’s value.
The find() method returns the corresponding key if the equality check succeeds and stops searching.

The find() method returns undefined if the equality check never returns true.
Let’s have an example –

function getObjectKey(obj, value) {
  return Object.keys(obj).find(key => obj[key] === value);
}

const obj = {country: 'England', city: 'London'};

console.log(getObjectKey(obj, 'NoMatchFound'));
The find method returns undefined if the key is not found in the array.
The find method returns undefined if the key is not found in the array.

The find() method returns the first element in the provided array that satisfies the provided testing function. If there are multiple keys with the same value, the find() method will return the name of the first key.

Let’s see an example –

 function getObjectKey(obj, value) {
  return Object.keys(obj).find(key => obj[key] === value);
}

const obj = {countryFirst: 'England', countrySecond: 'England'};

console.log(getObjectKey(obj, 'England'));
The find method always returns the first element that satisfies the testing function.
The find method always returns the first element that satisfies the testing function.

Here’s what the above code is doing:

  1. We create a function called getObjectKey that takes two arguments: obj and value.
  2. We use the Object.keys() method to get all the keys of the object.
  3. We use the find() method to find the first key that matches the value.
  4. We return the key.
  5. We create an object called obj.
  6. We call the getObjectKey function and pass in the obj and the value we want to find.
  7. We log the result to the console.

To get multiple object keys that store the same value:

  1. Use the Object.keys() method to get an array of the object’s keys.
  2. Call the Array.filter() method to get an array of all the matching values.

Example code –

function getObjectKey(obj, value) {
  return Object.keys(obj).filter(key => obj[key] === value);
}

const obj = {countryFirst: 'England', countrySecond: 'England'};

console.log(getObjectKey(obj, 'England'));
04. use Object.keys and Array.filter method to get multiple object keys that store the same value
Use the Object.keys() and Array.filter() methods to get multiple object keys that store the same value.

In this code example Object.keys(obj) returns an array of keys. filter(key => obj[key] === value) returns an array of keys that have the same value. So, the result is an array of keys that have the same value.

Here’s what the above code is doing:

  1. We have an object with two keys and both have the same value.
  2. We are passing the object and the value to the function.
  3. We are using the Object.keys() method to get all the keys of the object.
  4. We are using the filter() method to filter out the keys that have the same value.
  5. We are returning the filtered keys.

The function we passed to the filter() method returns a truthy value for each key in the array.