Ravi
Ravi

Reputation: 21

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.

Upvotes: 1

Views: 89

Answers (5)

ua Aleksey
ua Aleksey

Reputation: 1

It can be simplified, try this...

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

Upvotes: 0

Ehsan
Ehsan

Reputation: 195

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
}

Upvotes: 0

Neha Soni
Neha Soni

Reputation: 4684

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");
            }
        }

    }
}

Upvotes: 1

Hossein Mousavi
Hossein Mousavi

Reputation: 3814

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');
  }
});

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44087

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");
}

Upvotes: 1

Related Questions