Skip to content
Related Articles

Related Articles

Improve Article

Access Object property case-insensitively in JavaScript

  • Last Updated : 31 Jul, 2020

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :