[英]JavaScript nested object array, How to loop through the 'KEY' of 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 ) {
document.getElementById("id1").innerHTML += x + "<br />";
}
Ans : cars
Ans:汽车
Likewise how can I display the 'KEY' value which is inside the array ie
'name' or
'models'
同样,我如何显示数组内部的“ KEY”值, 即
“名称”或
“模型”
for ( x in myCollection.cars ) {
document.getElementById("id1").innerHTML += x + "<br />";
}
Ans :
答:
0
0
1
1
2
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
for loops are looping through each index, so you then have to take that index and drill into the array for循环遍历每个索引,因此您必须获取该索引并深入数组
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. myCollection.cars [x]是如何获取数组中的特定对象的方法,并从Object.keys()提供该对象的键。 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 />";
}
This for in
is mean't for object's. for in
的意思不是对象的。 You are using an array so its returning index. 您正在使用数组,因此其返回索引。 Instead use a forEach like shown below.
而是使用如下所示的forEach。
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. 注释下面的行将得到您想要的输出,您可以打印汽车或模型。
Here you go with a solution https://jsfiddle.net/sg6kj0n7/1/ 在这里您可以找到解决方案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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.