1

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.

1
  • what do you really want, check for a value in the array "Obj.CheckFilterSettings.values"? Commented Feb 16, 2021 at 6:01

5 Answers 5

1

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is wrong. Check - What would Obj[i] return? And you're trying to access CheckFilterSettings on Obj[i]
Please see the edited post @Tethys0 (I've amended it and added a note).
1

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

    }
}

Comments

0

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

Comments

0

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
}

Comments

0

It can be simplified, try this...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.