This question already has an answer here:
这个问题在这里已有答案:
- Why is using “for…in” with array iteration a bad idea? 25 answers
- 为什么在数组迭代中使用“for ... in”是一个坏主意? 25个答案
- JavaScript for…in vs for 23 answers
- JavaScript for ... in vs in 23 answers
I have a JSON String being parsed (in a response
var) from AJAX:
我从AJAX中解析了一个JSON字符串(在响应变量中):
The JSON
JSON
{
"TheArray":[
{
"AlmostThere": {
"whatWeAreLookingFor":"Hello"
}
},
{
"AlmostThere": {
"whatWeAreLookingFor":"Goodbye"
}
}
]
}
The JSON being parsed
正在解析的JSON
var jsonData = JSON.parse(response); //response is the string version of the JSON code!
Now, I need to loop into the JSON array, hereby mentioned as TheArray
. I do this:
现在,我需要循环到JSON数组,特此提到为TheArray。我这样做:
Looping TheArray
循环TheArray
for (var contents in jsonData["TheArray"]) {
}
And inside there, we get the value of the whatWeAreLookingFor
element:
在那里,我们得到了whatWeAreLookingFor元素的值:
for (var contents in jsonData["TheArray"]) {
console.log(contents.whatWeAreLookingFor + "!");
}
But there is a catch! The console outputs... undefined!
. - I have tried multiple ways to make this work, such as using contents["whatWeAreLookingFor"]
and what-not, but I still get the same results.
但是有一个问题!控制台输出......未定义! - 我已经尝试了多种方法来完成这项工作,例如使用内容[“whatWeAreLookingFor”]和什么不是,但我仍然得到相同的结果。
3 个解决方案
#1
3
You forgot to access AlmostThere
你忘了访问AlmostThere
jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor
for (var i = 0; i < jsonData.TheArray.length; i++) {
console.log(jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor);
}
#2
0
If you loop your array TheArray
in your way, the contents
var will become these two objects:
如果以你的方式循环你的数组TheArray,内容var将成为这两个对象:
{
"AlmostThere": {
"whatWeAreLookingFor":"Hello"
}
}
and
和
{
"AlmostThere": {
"whatWeAreLookingFor":"Goodbye"
}
}
Now you want to access the value with
现在您想要访问该值
contents.whatWeAreLookingFor
contents.whatWeAreLookingFor
but this attribute is undefined for these objects. So your console logs undefined
. You have to access the value with that:
但是对于这些对象,此属性未定义。所以你的控制台记录未定义。您必须使用以下内容访问该值:
contents.AlmostThere.whatWeAreLookingFor
So you get the object AlmostThere
and select the attribute whatWeAreLookingFor
.
因此,您获取对象AlmostThere并选择属性whatWeAreLookingFor。
If your using jquery you should use:
如果你使用jquery你应该使用:
$.each(jsonData.TheArray, function() {
console.log(contents.AlmostThere.whatWeAreLookingFor + '!');
});
API: http://api.jquery.com/jquery.each/
API:http://api.jquery.com/jquery.each/
#3
0
for... in
iterates over the keys of an object. For an array, that means (approximately) the indexes 0
, 1
, 2
, etc.
for ... in迭代对象的键。对于数组,这意味着(大约)索引0,1,2等。
You could use Array.prototype.forEach instead:
您可以使用Array.prototype.forEach代替:
jsonData.theArray.forEach(function(contents) {
console.log(contents.AlmostThere.whatWerAreLookingFor);
})
#1
3
You forgot to access AlmostThere
你忘了访问AlmostThere
jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor
for (var i = 0; i < jsonData.TheArray.length; i++) {
console.log(jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor);
}
#2
0
If you loop your array TheArray
in your way, the contents
var will become these two objects:
如果以你的方式循环你的数组TheArray,内容var将成为这两个对象:
{
"AlmostThere": {
"whatWeAreLookingFor":"Hello"
}
}
and
和
{
"AlmostThere": {
"whatWeAreLookingFor":"Goodbye"
}
}
Now you want to access the value with
现在您想要访问该值
contents.whatWeAreLookingFor
contents.whatWeAreLookingFor
but this attribute is undefined for these objects. So your console logs undefined
. You have to access the value with that:
但是对于这些对象,此属性未定义。所以你的控制台记录未定义。您必须使用以下内容访问该值:
contents.AlmostThere.whatWeAreLookingFor
So you get the object AlmostThere
and select the attribute whatWeAreLookingFor
.
因此,您获取对象AlmostThere并选择属性whatWeAreLookingFor。
If your using jquery you should use:
如果你使用jquery你应该使用:
$.each(jsonData.TheArray, function() {
console.log(contents.AlmostThere.whatWeAreLookingFor + '!');
});
API: http://api.jquery.com/jquery.each/
API:http://api.jquery.com/jquery.each/
#3
0
for... in
iterates over the keys of an object. For an array, that means (approximately) the indexes 0
, 1
, 2
, etc.
for ... in迭代对象的键。对于数组,这意味着(大约)索引0,1,2等。
You could use Array.prototype.forEach instead:
您可以使用Array.prototype.forEach代替:
jsonData.theArray.forEach(function(contents) {
console.log(contents.AlmostThere.whatWerAreLookingFor);
})