How to use the find Method() In Javascript

  • The Object.keys() method is used to return all the keys of the object.
  • On this array of keys, the find() method is used to test if any of these keys match the value provided.
  • The find() method is used to return the value of the first element that satisfies the testing function.
  • If the value matches, then this condition is satisfied and the respective key is returned. This is the key to the value of the object.

Note: This method was added to the ES6 specification and may not be supported on older browser versions.

Example: This example is the implementation of the above-explained approach.

Javascript
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

How to get a key in a JavaScript object by its value ?

In this article, we will learn how to get a key in a JavaScript object by its value. The values of the object can be found by iterating through its properties. Each of these properties can be checked to see if they match the value provided.

How to get a key in a JavaScript object by its value?


Below are the approaches through which we get a key in a JavaScript object by its value:

Table of Content

  • Using a for-in loop
  • Using the find Method()
  • Using filter() Method and Object keys() Method
  • Using Object.entries() and reduce() Method
  • Using Lodash _.findKey() Method
  • Using Object.values() and indexOf() Method

Similar Reads

Method 1: Using a for-in loop

The values of the object can be found by iterating through its properties. Each of these properties can be checked to see if they match the value provided. The properties of the object are obtained by using a for loop on the object. These properties are then checked with the object’s hasOwnProperty() method to make sure it is a direct property of the object and not an inherited one. Each property is then checked if they are equal to the value to be found. If the value matches, then the property is returned. This is the key to the value of the object....

Method 2: Using the find Method()

The Object.keys() method is used to return all the keys of the object. On this array of keys, the find() method is used to test if any of these keys match the value provided. The find() method is used to return the value of the first element that satisfies the testing function. If the value matches, then this condition is satisfied and the respective key is returned. This is the key to the value of the object....

Using filter() Method and Object keys() Method

In this method, we will get use object.keys and filter() methods to get a key in JavaScript by its value.we will filter the given key and return it’s value if it present in the object....

Using Object.entries() and reduce() Method

In this method, we will get use object.entries() and reduce() methods to get a key in JavaScript by its value.And return the value of the given key....

Using Lodash _.findKey() Method

In this method we are using the _.findkey() method of lodash.This returns the key of the given object....

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()....