How to get the value of a nested object containing array of values

I am trying to check if a value is present in the below nested object:

var Obj = {
   "testname":"Validator",
   "dataTableName":"y",
   "dataColumnName":"y",
   "CheckFilter":"CheckBoxFilter",
   "CheckFilterSettings":{
      "values":[
         "70.00"
      ],
      "hierarchyPaths":[
         
      ]
   }
}

I tried using

for (var i in Obj) {
    if (Obj[i].CheckFilterSettings.values[0] === "70.00"){
    console.log("Value Found");
    }else{
        console.log("Value Not Found");
        }
    }

Could someone please suggest where I am going wrong.

5 thoughts on “How to get the value of a nested object containing array of values”

  1. You can use Object.keys() like this:

    let Obj = {
        "testname": "Validator",
        "dataTableName": "y",
        "dataColumnName": "y",
        "CheckFilter": "CheckBoxFilter",
        "CheckFilterSettings": {
            "values": [
                "70.00"
            ],
            "hierarchyPaths": [
    
            ]
        }
    }
    
    Object.keys(Obj).map((key) => {
      if (key == 'CheckFilterSettings') {
        Obj[key].values[0] == '70.00' ? console.log('value found') : console.log('value not found');
      }
    });
    Reply
  2. If you want to check that the values array is not empty, and that the path to that array remains constant, you could just use:

    for (var i in Obj) {
        if (Obj[i].CheckFilterSettings.values.length) {
        console.log("Value Found");
        } else {
            console.log("Value Not Found");
        }
    }
    

    Note that this assumes an array of objects, which is not evident from the provided data structure. If that’s the entire object, then try:

    if (Obj.CheckFilterSettings.values.length) {
        console.log("Value Found");
    } else {
        console.log("Value Not Found");
    }
    
    Reply
  3. Your Obj[i] is itself the key, so, do not call "CheckFilterSettings" on Obj[i] again.

    Check this one-

    var Obj = {
        "testname": "Validator",
        "dataTableName": "y",
        "dataColumnName": "y",
        "CheckFilter": "CheckBoxFilter",
        "CheckFilterSettings": {
            "values": [
                "70.00"
            ],
            "hierarchyPaths": [
    
            ]
        }
    }
    
    for (var i in Obj) {
        if (Obj.hasOwnProperty(i)) {
            if (i === "CheckFilterSettings" && Obj[i].hasOwnProperty("values")) {
                if (Obj[i].values[0] === "70.00") {
                    console.log("Value Found");
                } else {
                    console.log("Value Not Found");
                }
            }
    
        }
    }
    Reply
  4. no need to loop through the object. just simply use es20 feature optional chaining:

    var Obj = {
       "testname":"Validator",
       "dataTableName":"y",
       "dataColumnName":"y",
       "CheckFilter":"CheckBoxFilter",
       "CheckFilterSettings":{
          "values":[
             "70.00"
          ],
          "hierarchyPaths":[
             
          ]
       }
    }
    
    if(Obj?.CheckFilterSettings?.values?.length>0){
    console.log(Obj?.CheckFilterSettings?.values)
    }else {
      //whatever you want
    }
    Reply
  5. It can be simplified, try this…

    if (Obj.CheckFilterSettings && Obj.CheckFilterSettings.values[0] === "70.00"){
        console.log("Value Found");
    } else {
        console.log("Value Not Found");
    }
    
    Reply

Leave a Comment