0
var myCollection = {
"cars": [
          { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
          { "name":"BMW", "models":[ "320", "X3", "X5" ] },
          { "name":"Fiat", "models":[ "500", "Panda" ] }
        ]
}
for ( x in myCollection ) {
  document.getElementById("id1").innerHTML += x + "<br />";
}

Ans : cars
Likewise how can I display the 'KEY' value which is inside the array i.e. 'name' or 'models'

for ( x in myCollection.cars ) {
  document.getElementById("id1").innerHTML += x + "<br />"; 
}

Ans :
0
1
2

Why it returns array index value, How can I display the 'KEY' value 'name' or 'models' ?

Expected Ans:

name      models 
name OR models
name models

3 Answers 3

1

for loops are looping through each index, so you then have to take that index and drill into the array

var myCollection = {
"cars": [
          { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
          { "name":"BMW", "models":[ "320", "X3", "X5" ] },
          { "name":"Fiat", "models":[ "500", "Panda" ] }
        ]
}
for ( x in myCollection.cars ) {
  var keys = Object.keys(myCollection.cars[x])
  document.getElementById("id1").innerHTML += keys[0] + "<br />";
}

the myCollection.cars[x] is how you get the specific object in the array and from the Object.keys() gives you the keys of that object. If you want the value and the key

for ( x in myCollection.cars ) {
  var keys = Object.keys(myCollection.cars[x])
  document.getElementById("id1").innerHTML += 
     keys[0] + " : " + myCollection.cars[x].name + " <br />" +
     keys[1] + " : " + myCollection.cars[x].models + "<hr />";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
myCollection.cars[keys[0]] returns undefined but keys[0] and keys[1] works fine as I expected.
@NikolayBaranenko changed it to var so you wouldnt have that issue
0

This for in is mean't for object's. You are using an array so its returning index. Instead use a forEach like shown below.

var myCollection = {
    "cars": [
              { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
              { "name":"BMW", "models":[ "320", "X3", "X5" ] },
              { "name":"Fiat", "models":[ "500", "Panda" ] }
            ]
    }
    myCollection.cars.forEach(function (value) {
      //console.log("name" + value.name + ", models" + value.models);
      //for printing cars alonecars
      console.log(value.name);
      //for print models alone
      value.models.forEach(function (model) {
        console.log(model);
      });
    });

The line below comments will get your desired output, you can print either cars or models.

4 Comments

I know how to display a string value, I want to know how to LOOP through the 'KEY' or 'NAME' value of the nested array. Please revise the Expected Answers in my Question.
@JeevaRaam Updated my answer can you please check?
'for in' loop is more dynamic to loop through the 'value' values and I know how to do it, but what I was expecting is to loop the 'name' values.
@JeevaRaam Glad you found the answer :)
0

Here you go with a solution https://jsfiddle.net/sg6kj0n7/1/

var myCollection = {
"cars": [
          { "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
          { "name":"BMW", "models":[ "320", "X3", "X5" ] },
          { "name":"Fiat", "models":[ "500", "Panda" ] }
        ]
}
for ( var x in myCollection.cars ) {
	for (var key in myCollection.cars[x]) {
 	 	document.getElementById("id1").innerHTML += key + "   ";
  }
  document.getElementById("id1").innerHTML += "<br/>";
}
<div id ="id1">

</div>

2 Comments

I know how to loop through the 'VALUE' value, I want to know how to loop through the 'KEY' or 'NAME' value of the nested array.
@JeevaRaam . I have updated the answer. Solution will work irrespective of N number of keys.

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.