Friday, September 5, 2025
HomeLanguagesJavascriptAccess Object property case-insensitively in JavaScript

Access Object property case-insensitively in JavaScript

In this article, we will see how to access an object’s properties without case-sensitivity, In this article, we are discussing how to access object property case-insensitively in JavaScript.

Method 1: Using toLowerCase() and Object.hasOwnProperty() Methods

In this approach, the key is passed to the function and while comparing, use the toLowerCase() method to transform the passed key as well as the object’s key to lowercase. Then compare the keys and if they are the same then return their value.

Example: This example shows the use of the above-explained approach.

Javascript




let GFG_Object = {
    a: "Val_1",
    n: "Val_2",
    c: "Val_3",
    b: "Val_4",
};
 
function findVal(obj, prop) {
    prop = (prop + "").toLowerCase();
    for (let p in obj) {
        if (obj.hasOwnProperty(p) && prop ==
            (p + "").toLowerCase()) {
            return obj[p];
        }
    }
}
 
function GFG_Fun() {
    let key = "A";
 
    console.log("Key: '" + key
        + "'\nValue: '"
        + findVal(GFG_Object, key)
        + "'");
}
 
GFG_Fun();


Output

Key: 'A'
Value: 'Val_1'

Method 2: Using Object.keys() and toLowerCase() Methods

The approach here is the same as in the previous example, The Object.keys() method is used and the operation is performed in a compact manner.

Example: This example shows the use of the above-explained approach.

javascript




let GFG_Object = {
    a: "Val_1",
    n: "Val_2",
    c: "Val_3",
    b: "Val_4",
};
 
function GFG_Run() {
    let ObjKey = "A";
     
    console.log("Key: '" + ObjKey
        + "'\nValue: '"
        + GFG_Object[Object.keys(GFG_Object).find(
            (key) => key.toLowerCase() === ObjKey.toLowerCase())]
        + "'");
}
 
GFG_Run();


Output

Key: 'A'
Value: 'Val_1'
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS