Today we will introduce how to get an object’s key by its value using JavaScript. We have already tested effectively by using the find()
and filter()
methods. Hopefully, throughout this article, you can find for yourself the best way to get an object’s key by its value.
Get An Object’s Key By Its Value Using JavaScript
Using the find()
method
The first way we want to introduce in this article to get an object’s key by its value using javascript is using the find() method. This method is used to find the first element of the array that passes our provided condition, and it does not execute the function for empty elements.
Code example:
In this example, we get an object’s key by its value following this way.
- We call out the
Object.Key()
method to get the object key. - After that, get the key associated with the specified value by using the
find()
method.
Let’s see the code example below.
var greetings = { English: "hello", Ukrainian: "привіт", France: "bonjour", Japanes: "こんにちは", // Kon'nichiwa }; console.log(greetings); // Call the Object.keys(), then use the find() to find the key associated with the specified value function getKeyByValue(object, value) { return Object.keys(object).find((key) => object[key] === value); } console.log("Get an Object's Key by its Value"); const key = getKeyByValue(greetings, "hello"); console.log("value 'hello' => key:", key); // English
Output
{English: 'hello', Ukrainian: 'привіт', France: 'bonjour', Japanes: 'こんにちは'}
Get an Object's Key by its Value
Java Script/index.js:14
value 'hello' => key: English
Using the filter()
method
The second way is using the filter() method. This method creates a shallow copy of a portion of a given array, and this method helps find out all keys that the value contains.
Code example:
In this example, we get an object’s key by its value following this way.
- We call out the
Object.Key()
method to get the object key. - After that, instead of using the
find()
method, we use thefilter()
method to get all the keys that correspond to a certain value.
Let’s see the code example below.
var greetings = { English: "hello", USA: "hello", Ukranian: "привіт", France: "bonjour", Japanes: "こんにちは", // Kon'nichiwa }; console.log(greetings); // Using the filter method to get all keys that correspond to contain value function getKeyByValue(object, value) { return Object.keys(object).filter((key) => object[key] === value); } console.log("Get an Object's Key by its Value"); const key = getKeyByValue(greetings, "hello"); console.log("value 'hello' => key:", key); // English, USA
Output
{English: 'hello', USA: 'hello', Ukranian: 'привіт', France: 'bonjour', Japanes: 'こんにちは'}
Get an Object's Key by its Value
value 'hello' => key: (2) ['English', 'USA']
Summary
In this tutorial, we have explained how to get an object’s key by its value using javascript by using the filter()
method and find()
method. But we think using the filter method is helpful and easy to use. That’s what I would like to introduce to you. Hope the article is helpful to you. If you have any comments on the article, leave me a comment below.
Thank you for your reading!
Maybe you are interested:
- Sort the Keys of an Object in JavaScript
- Get an Object’s Value by Variable Key in JavaScript
- Swap the Keys and Values in an Object in JavaScript
- Add a key/value pair to an object in javascript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python