How To Get An Object’s Key By Its Value Using JavaScript

Get an Object's Key by its Value using JavaScript

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.

  1. We call out the Object.Key() method to get the object key.
  2. 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.

  1. We call out the Object.Key() method to get the object key.
  2. After that, instead of using the find() method, we use the filter() 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:

Leave a Reply

Your email address will not be published. Required fields are marked *