0

I have a JSON object and I am iterating through it. I am using different values from different levels of it.

But I am not able to create path dynamically to reiterate the object.

var data= {
  "algoName": "textClassification",
  "hyperParams": {
    "mode": {
      "data_type": "string",
      "default_value": "supervised",
      "required": true,
      "description": "The training mode",
      "allowedValues": [
        "supervised",
        "unsupervised"
      ]
    }
  }
}

var key1;
for(var key in data.hyperParams) {

  key1=key;

}
var text1 =  "data.hyperParams" + key1

for(var key in text1) {
  console.log(text1[key]);
}
Hemant
  • 391
  • 1
  • 2
  • 10
  • 1
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Feb 19 '19 at 13:14
  • I am trying to iterate through object but "for(var key in text1)" is not working properly it is taking text1 as sting not a path. So instead of giving values it is giving letters that makes the text1 string. – Hemant Feb 19 '19 at 13:18
  • I can read your question at least two ways. I'm fairly sure you want to use a string path to access a nested value; that's what [the linked duplicate](https://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key) does. But if you just want to access a non-nested property by string name instead, [this one](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) would be a better fit. – T.J. Crowder Feb 19 '19 at 13:19

1 Answers1

0

Thank you T.J. Crowder. I modified code as following and it is working now.

for(var key in data.hyperParams[key1]) {
  console.log(data.hyperParams[key1][key]);
}
Hemant
  • 391
  • 1
  • 2
  • 10