[英]Looping through an array of JSON objects in Javascript
I orginally was going to ask a question on why it wasn't working. 我原本打算问一个问题,为什么它不起作用。 But as I commented the code I realized that I could access the text array style and
item
was giving me the index. 但是当我评论代码时,我意识到我可以访问文本数组样式,而
item
正在给我索引。 It took me a while to find this as the solution, however I would have much rather used item.text
in the for loop. 我花了一段时间才发现这是解决方案,但是我会更多地使用for循环中的
item.text
。 Is this [my posted answer] the valid way to loop through JSON objects in Javascript? 这个[我发布的答案]是在Javascript中循环JSON对象的有效方法吗?
There's no such thing as a JSON object - once you've parsed your JSON (which is a string) you end up with simply an object. 没有JSON对象这样的东西 - 一旦你解析了你的JSON(这是一个字符串),你最终只会得到一个对象。 Or an array.
或者数组。
Anyway, to loop through an array of objects, use a traditional for
loop. 无论如何,要遍历一个对象数组,请使用传统的
for
循环。 A for..in
loop may work, but is not recommended because it may loop through non-numeric properties (if somebody has been messing with the built-in array). for..in
循环可能有效,但不推荐使用,因为它可能循环遍历非数字属性(如果某人一直在搞乱内置数组)。
In your specific case, if obj.body.items
is an array then do this: 在您的特定情况下,如果
obj.body.items
是一个数组,那么执行以下操作:
for (var i = 0; i < obj.body.items.length; i++) {
// obj.body.items[i] is current item
console.log(obj.body.items[i].text);
}
You can also, arguably, make the code a bit neater by keeping a reference directly to the array rather than accessing it via the whole obj.body.
通过将引用直接保存到数组而不是通过整个
obj.body.
访问它,您可以说,使代码更整洁obj.body.
chain every time: 每次链:
var items = obj.body.items;
for (var i = 0; i < items.length; i++) {
console.log(items[i].text);
}
"I would have much rather used
item.text
in the for loop"“我会更多地使用 for循环中的
item.text
”
In your answer - which really should've been posted as part of the question - because you are using a for..in
loop your item
variable is being set to each index in turn as a string, eg, on the first iteration it will be the string "0"
, and strings don't have a text
property so item.text
doesn't work - although it should give you undefined
, not null
. 在您的答案中 - 实际上应该将其作为问题的一部分发布 - 因为您正在使用
for..in
循环,您的item
变量将依次设置为每个索引作为字符串,例如,在第一次迭代时它将是字符串"0"
,字符串没有text
属性,因此item.text
不起作用 - 虽然它应该给你undefined
,而不是null
。 But you can do this: 但你可以这样做:
var item;
for (var i = 0; i < obj.body.items.length; i++) {
item = obj.body.items[i] // get current item
console.log(item.text); // use current item
}
That is, declare a variable called item
that you set to reference the current array item at the beginning of each loop iteration. 也就是说,声明一个名为
item
的变量,您设置为在每次循环迭代开始时引用当前数组项。
<script type="text/javascript">
...
obj = JSON.parse(xmlhttp.responseText);
// displays the object as expected
console.log(obj);
// display the string next as expected
console.log(obj.body.next);
// displays the text of the item as expected
console.log(obj.body.items[0].text);
for (var item in obj.body.items) {
// displays the index value of the item
console.log(item);
// displays null - WHAT?? HUH?
console.log(item.text);
// displays the text - finally
console.log(obj.body.items[item].text);
}
<script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.