How to access object’s properties without case-sensitivity, In this articles we are discussing how to access object property case-insensitively in JavaScript.
Approach 1: In this approach, the key is passed to the function and while comparing, Use toLowerCase() method to transform the passed key as well as object’s key to lowercase. Then compare the keys and if they are same then return its value.
Example:
<!DOCTYPE html> <html> <head> <title> Access Object property case-insensitively in JavaScript </title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var GFG_Object = { a: "Val_1", n: "Val_2", c: "Val_3", b: "Val_4", }; el_up.innerHTML = "Click on the button to access object "+ "case-insensitively.<br>Object - " + JSON.stringify(GFG_Object); function findVal(obj, prop) { prop = (prop + "").toLowerCase(); for (var p in obj) { if (obj.hasOwnProperty(p) && prop == (p + "").toLowerCase()) { return obj[p]; } } } function gfg_Run() { var key = "A"; el_down.innerHTML = "Key - '" + key + "'<br>Value - '" + findVal(GFG_Object, key) + "'"; } </script> </body> </html> |
Output:

Approach 2: The approach here is same as in previous example, The Object.keys() method is used and the operation is performed in a compact manner.
Example:
<!DOCTYPE html> <html> <head> <title> Access Object property case-insensitively in JavaScript </title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksForGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Click Here </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var GFG_Object = { a: "Val_1", n: "Val_2", c: "Val_3", b: "Val_4", }; el_up.innerHTML = "Click on the button to access object "+ "case-insensitively.<br>Object - " + JSON.stringify(GFG_Object); function gfg_Run() { var ObjKey = "A"; el_down.innerHTML = "Key - '" + ObjKey + "'<br>Value - '" + GFG_Object[Object.keys(GFG_Object).find((key) => key.toLowerCase() === ObjKey.toLowerCase())]; +"'"; } </script> </body> </html> |
Output:

Recommended Posts:
- How to access an object having spaces in the object's key using JavaScript ?
- How to access first value of an object using JavaScript ?
- JavaScript TypeError - Can't access property "X" of "Y"
- How to remove a property from JavaScript object ?
- JavaScript | Get the index of an object by its property.
- JavaScript TypeError - Can't assign to property "X" on "Y": not an object
- How to get all property values of a JavaScript Object (without knowing the keys) ?
- Basics of SOAP - Simple Object Access Protocol
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to Access <tr> element from Table using JavaScript ?
- How to check a JavaScript Object is a DOM Object ?
- JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization
- How to get a key in a JavaScript object by its value ?
- Object.is( ) in JavaScript
- Map vs Object in JavaScript
- How to get the first key name of a JavaScript object ?
- CSS | The object-fit Property
- Object.freeze( ) in JavaScript
- JavaScript | Object Accessors
- JavaScript | Set object key by variable
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.


