2

I'm trying to parse the following data:

data = [       
   {"id":"orderBy", "options" : [
       {"value":"order-by=newest&", "name":"newest"},
       {"value":"order-by=relevance&", "name":"relevance"}
   ]},
   {"id":"searchBy", "options" : [
       {"value":"search-by=name&", "name":"name"},
       {"value":"search-by=number&", "name":"number"},
       {"value":"search-by=date&", "name":"date"},
       {"value":"search-by=location&", "name":"location"}
   ]}
];

Using this data I want to iterate through each object and return its ID, and then all of it options.

I know I need to use for loop like this:

for (var i =0; i < data.length; i++) {

  var id = data[i].id;
  var optionText = data[i].options[i].text;
  var optionValue = data[i].options[i].value;

  console.log(id);
  console.log(optionText);
  console.log(optionValue); 
}:

This loop only returns the first OptionText & OptionValue from the data. Can you teach me what I'm doing wrong?

Thanks.

4
  • 2
    you are on the right track, you just need to do another foreach for the options, right now you are using the same i from the original loop. Commented Nov 14, 2016 at 18:09
  • 4
    Couple of things: you don't have JSON. JSON is a string format that needs to be parsed before you use it. What you have is an array of objects. You also are not parsing it. Parsing is (usually) taking something from a string format and putting it into a format that is used by your program. As for how to get the right data, you seem to understand how to use for loops to go through the first level. Could you use another for loop to get the inner items? Commented Nov 14, 2016 at 18:10
  • you have no option text inside, but a name in the object. Commented Nov 14, 2016 at 18:15
  • thanks peeps, those answers were educational! Commented Nov 14, 2016 at 18:19

3 Answers 3

4

You could use another for loop for the inner array.

var data = [{ "id": "orderBy", "options": [{ "value": "order-by=newest&", "name": "newest" }, { "value": "order-by=relevance&", "name": "relevance" }] }, { "id": "searchBy", "options": [{ "value": "search-by=name&", "name": "name" }, { "value": "search-by=number&", "name": "number" }, { "value": "search-by=date&", "name": "date" }, { "value": "search-by=location&", "name": "location" }] }],
    i, j, id, optionText, optionValue;

for (i = 0; i < data.length; i++) {
    id = data[i].id;
    for (j = 0; j < data[i].options.length; j++) {
        optionText = data[i].options[j].name;
        optionValue = data[i].options[j].value;
        console.log(id, optionText, optionValue); 
    }
}

Sign up to request clarification or add additional context in comments.

Comments

1

data = [       
   {"id":"orderBy", "options" : [
       {"value":"order-by=newest&", "name":"newest"},
       {"value":"order-by=relevance&", "name":"relevance"}
   ]},
   {"id":"searchBy", "options" : [
       {"value":"search-by=name&", "name":"name"},
       {"value":"search-by=number&", "name":"number"},
       {"value":"search-by=date&", "name":"date"},
       {"value":"search-by=location&", "name":"location"}
   ]}
];
    
for (var i in data) {
  var id = data[i].id;
  console.log(id)
  for (var opt of data[i].options){
    console.log(opt.value);
    console.log(opt.name)
  }

}

You lacked an interation loop. Also I believe it could be the perfect example for you to see the difference (and existence?) of iterating through objects and arrays with in and of

Comments

1

You have two levels of arrays you need to loop through. First loop through the outer array, and then through the options of each item:

var data = [{"id":"orderBy","options":[{"value":"order-by=newest&","name":"newest"},{"value":"order-by=relevance&","name":"relevance"}]},{"id":"searchBy","options":[{"value":"search-by=name&","name":"name"},{"value":"search-by=number&","name":"number"},{"value":"search-by=date&","name":"date"},{"value":"search-by=location&","name":"location"}]}];

for (var i = 0; i < data.length; i++) {
  var id = data[i].id;
  console.log(id);
  
  for(var j = 0; j < data[i].options.length; j++) {
    var optionText = data[i].options[j].name;
    var optionValue = data[i].options[j].value;
    console.log(optionText);
    console.log(optionValue);
  }
}

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.