How to Get an Object's Key by its Value in JavaScript
In JavaScript, objects provide a direct way to get a value from a key (obj[key]), but there is no built-in method to do the reverse: get a key from a value. This is a common requirement, for example, when you have a "lookup" object and need to find the name of the key that corresponds to a specific value.
This guide will teach you the modern, standard methods for solving this problem. You will learn how to find the first matching key and how to find all matching keys using functional array methods.
The Core Logic: Iterating Over Keys
Since objects don't have a reverse lookup method, the solution is to get a list of all the object's keys and then iterate through them, checking the value associated with each key.
- Get the Keys: Use
Object.keys(obj)to get an array of all the object's keys. - Iterate and Check: Loop through the keys. For each
key, check ifobj[key]is equal to the value you are searching for.
Scenario 1: How to Find the First Key That Matches a Value
If you only need to find the first key that corresponds to a given value, the Array.prototype.find() method is the most concise and efficient tool.
We have a user object and we need to find the key that holds the value "Alice".
// Problem: What is the key for the value "Alice"?
const users = {
employee1: 'Alice',
employee2: 'Bob',
manager: 'Alice',
};
Solution: the find() method iterates over an array and returns the first element that satisfies the provided testing function. It stops as soon as a match is found.
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
// Example Usage:
const users = {
employee1: 'Alice',
employee2: 'Bob',
manager: 'Alice',
};
const key = getKeyByValue(users, 'Alice');
console.log(key); // Output: "employee1"
// If no match is found, find() returns undefined.
const missingKey = getKeyByValue(users, 'Charlie');
console.log(missingKey); // Output: undefined
This is the best practice for finding a single key.
Scenario 2: How to Find All Keys That Match a Value
If multiple keys in your object can have the same value, you might need to find all of them. For this, the Array.prototype.filter() method is the correct tool.
For example, we need to find all keys that hold the value "Alice".
// Problem: Which keys have the value "Alice"?
const users = {
employee1: 'Alice',
employee2: 'Bob',
manager: 'Alice',
};
The filter() method creates a new array containing all elements that pass the test.
function getAllKeysByValue(object, value) {
return Object.keys(object).filter(key => object[key] === value);
}
// Example Usage:
const users = {
employee1: 'Alice',
employee2: 'Bob',
manager: 'Alice',
};
const keys = getAllKeysByValue(users, 'Alice');
console.log(keys); // Output: ['employee1', 'manager']
A Note on Performance and Data Structures
Searching for a key by its value requires iterating through the object's keys, which can be slow for very large objects. If you find yourself performing this "reverse lookup" operation frequently, it might be a sign that your data structure is not optimal for your use case.
Alternative: For a high-performance, two-way lookup, a better solution is to use two Map objects.
const nameToId = new Map([
['Alice', 'employee1'],
['Bob', 'employee2'],
]);
const idToName = new Map([
['employee1', 'Alice'],
['employee2', 'Bob'],
]);
// Both lookups are now extremely fast.
console.log(idToName.get('employee1')); // 'Alice'
console.log(nameToId.get('Alice')); // 'employee1'
Conclusion
Getting an object's key by its value is a common task that can be solved cleanly with modern JavaScript array methods.
- To find the first matching key, the most direct and efficient method is
Object.keys(obj).find(...). - To find all matching keys, the correct tool is
Object.keys(obj).filter(...). - If you need to perform this operation frequently on large datasets, consider if a different data structure, such as two
Maps, would be a more performant choice.