How to get a key in a JavaScript object by its value ?
Method 1: Checking all the object properties to find the value: The values of the object can be found by iterating through its properties. Each of these properties con 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:
<!DOCTYPE html> <html> <head> <title> How to get a key in a JavaScript object by its value ? </title> </head> <body> <h1 style="color: green">GeeksforGeeks</h1> <b> How to get a key in a JavaScript object by its value ? </b> <p>Getting the key of the value '100'.</p> <p>See the console for the output</p> <script> function getKeyByValue(object, value) { for (var prop in object) { if (object.hasOwnProperty(prop)) { if (object[prop] === value) return prop; } } } var exampleObject = { key1: 'Geeks', key2: 100, key3: 'Javascript' }; ans = getKeyByValue(exampleObject, 100); console.log(ans); </script> </body> </html> |
Output:

Console Output:

Method 2: Using the find method() to compare the keys: 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 in the ES6 specification and may not be supported on older browser versions.
Syntax:
function getKeyByValue(object, value) { return Object.keys(object).find(key => object[key] === value); } |
Example:
<!DOCTYPE html> <html> <head> <title> How to get a key in a JavaScript object by its value ? </title> </head> <body> <h1 style="color: green">GeeksforGeeks</h1> <b>How to get a key in a JavaScript object by its value?</b> <p>Getting the key of the value 'Geeks'.</p> <p>See the console for the output</p> <script> function getKeyByValue(object, value) { return Object.keys(object).find(key => object[key] === value); } var exampleObject = { key1: 'Geeks', key2: 100, key3: 'Javascript' }; ans = getKeyByValue(exampleObject, 'Geeks'); console.log(ans); </script> </body> </html> |
Output:

Console Output:

Recommended Posts:
- How to access an object having spaces in the object's key using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- Object.is( ) in JavaScript
- Map vs Object in JavaScript
- How to get the first key name of a JavaScript object ?
- JavaScript | History object
- How to iterate over a JavaScript object ?
- JavaScript | Object Methods
- Rename object key in JavaScript
- JavaScript | ArrayBuffer Object
- JavaScript | Set object key by variable
- How to add an object to an array in JavaScript ?
- How to add key/value pair to a JavaScript object?
- How to get the last item of JavaScript object ?
- Object.seal( ) In JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



