Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

Get-a-key-in-a-JavaScript-object-by-its-value

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:

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.

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

Javascript

function getKeyByValue(object, value) {
    for (let prop in object) {
        if (object.hasOwnProperty(prop)) {
            if (object[prop] === value)
                return prop;
        }
    }
}
 
const exampleObject = {
    key1: 'Geeks',
    key2: 100,
    key3: 'Javascript'
};
 
ans = getKeyByValue(exampleObject, 100);
 
console.log(ans);

                    

Output
key2

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.

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

Method 3: 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.

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

Javascript

function getKeyByValue(obj, value) {
    return Object.keys(obj)
           .filter(key => obj[key] === value);
}
 
const exampleObject = {
    key1: 'Geeks',
    key2: 100,
    key3: 'Javascript'
};
 
ans = getKeyByValue(exampleObject, 'Geeks');
console.log(ans);

                    

Output
[ 'key1' ]

Method 4: 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.

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

Javascript

function getKeyByValue(obj, value) {
    return Object.entries(obj)
    .reduce((acc, [key, val]) => {
        if (val === value) {
            acc.push(key);
        }
        return acc;
    }, []);
}
 
const exampleObject = {
    key1: 'Geeks',
    key2: 100,
    key3: 'Javascript'
};
 
ans = getKeyByValue(exampleObject, 'Geeks');
console.log(ans);

                    

Output
[ 'key1' ]

Method 5: Using Lodash _.findKey() Method

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

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

Javascript

// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let users = {
    'meetu': { 'salary': 36000, 'active': true },
    'teetu': { 'salary': 40000, 'active': false },
    'seetu': { 'salary': 10000, 'active': true }
};
 
// Using the _.findKey() method
// The `_.matches` iteratee shorthand
let found_elem = _.findKey(users, {
    'salary': 10000,
    'active': true
});
 
// Printing the output
console.log(found_elem);

                    

Output:

seetu


Last Updated : 30 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads