Last updated: Mar 3, 2024
Reading timeยท4 min
To get an object's key by its value:
Object.keys()
method to get an array of the object's keys.find()
method to find the key that corresponds to the value.find
method will return the first key that meets the condition.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"
We used the Object.keys() method to get an array containing the object's keys.
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.
find()
method gets called with each element (key) in the array until it returns a truthy value or iterates over the entire array.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
.
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
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.
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
To get multiple object keys that store the same value:
Object.keys()
method to get an array of the object's keys.Array.filter()
method on the array of keys.filter()
method will return a new array containing all the matching
keys.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.
for...of
loopThis is a three-step process:
for...of
loop to iterate over the object's keys.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
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.
for..of
This is a four-step process:
for...of
loop to iterate over the Object's keys.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'));
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.
You can learn more about the related topics by checking out the following tutorials: