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

23 Aug, 2019
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:
javascript-object
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Roadmap to Learn JavaScript For Beginners
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Hide or show elements in HTML using display property
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to fetch data from an API in ReactJS ?
Roadmap to Learn JavaScript For Beginners