How to get a key in a JavaScript object by its value ?
To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.

Below are the approaches through which we get a key in a JavaScript object by its value:
Table of Content
1. Using a for-in Loop
The for-in loop iterates over an object's enumerable properties. You can compare each property’s value with the target value. If a match is found, return the key; otherwise, continue the loop until all properties are checked.
Example: In this example The for loop iterates over all properties in the exampleObject. It checks if the current property's value matches 100. If true, it returns the corresponding key (key2).
function getKeyByValue(object, value) {
for (let prop in object) {
if (object.hasOwnProperty(prop)) {
if (object[prop] === value)
return prop;
}
}
}
const exampleObject = {
key1: 'Geeks',
key2: 100,
key3: 'Javascript'
};
ans = getKeyByValue(exampleObject, 100);
console.log(ans);
Output
key2
2. Using the find() Method
The find() method can be used with Object.keys() to find a key by its value. First, convert the object’s keys into an array using Object.keys(). Then, use find() to locate the key whose value matches the target value, returning the key.
Note: This method was added to the ES6 specification and may not be supported on older browser versions.
Example: In this example this function uses Object.keys() to get all keys, then uses find() to locate the key whose value matches 'Geeks'. It returns the first matching key (key1).
function getKeyByValue(object, value) {
return Object.keys(object).find(key =>
object[key] === value);
}
const exampleObject = {
key1: 'Geeks',
key2: 100,
key3: 'Javascript'
};
ans = getKeyByValue(exampleObject, 'Geeks');
console.log(ans);
Output
key1
3. Using Object.entries() and reduce() Method
The Object.entries() method converts an object into an array of key-value pairs. Using `reduce()`, you can iterate over these pairs to find the key with the matching value. If the value matches, return the key; otherwise, continue accumulating until found.
Example: This example is the function uses Object.entries() to get key-value pairs, then applies reduce() to collect all keys whose values match 'Geeks', returning an array with the matching keys (['key1']).
function getKeyByValue(obj, value) {
return Object.entries(obj)
.reduce((acc, [key, val]) => {
if (val === value) {
acc.push(key);
}
return acc;
}, []);
}
const exampleObject = {
key1: 'Geeks',
key2: 100,
key3: 'Javascript'
};
ans = getKeyByValue(exampleObject, 'Geeks');
console.log(ans);
Output
[ 'key1' ]
4. Using Lodash _.findKey() Method
Lodash’s _.findKey() method searches an object and returns the first key whose value matches a given condition. Pass the object and a predicate function that checks the value; it returns the key when the condition is met.
Example: This example is the _.findKey() method from Lodash is used to find the key of an object (users) that matches the condition { salary: 10000, active: true }, returning 'seetu'.
// Requiring the lodash library
const _ = require("lodash");
// Original array
let users = {
'meetu': { 'salary': 36000, 'active': true },
'teetu': { 'salary': 40000, 'active': false },
'seetu': { 'salary': 10000, 'active': true }
};
// Using the _.findKey() method
// The `_.matches` iteratee shorthand
let found_elem = _.findKey(users, {
'salary': 10000,
'active': true
});
console.log(found_elem);
Output:
seetu
5. Using Object.values() and indexOf() Method
In this method, we'll leverage the Object.values() method to extract all the values from the object and then use the indexOf() method to find the index of the target value in the array of values. Once we have the index, we can use it to retrieve the corresponding key from the array of keys returned by Object.keys().
Example: The getKeyByValue function finds and returns the key matching the value in an object, or null if not found.
function getKeyByValue(object, value) {
// Get array of object values
const values = Object.values(object);
// Find the index of the target value
const index = values.indexOf(value);
// If the value is found
if (index !== -1) {
// Get array of object keys
const keys = Object.keys(object);
// Return the key at the same index
return keys[index];
}
// If value is not found, return null or handle accordingly
return null;
}
let obj = {
name: "Alice",
age: 25,
city: "London"
};
// Get the key for the value "London"
console.log(getKeyByValue(obj, "London"));
Output
city