在Javascript中循环对象[重复]

时间:2022-10-30 10:28:21

Possible Duplicate:
Loop through Json object

可能重复:循环通过Json对象

{"data":[{"name":"Jen","id":"1"},{"name":"Steve","id":"8"}]}

A server I'm interacting with responds with the above.

我正在与之交互的服务器以上述方式响应。

I'm trying to loop through it for the For..in statement.

我正在尝试为For..in语句循环遍历它。

This is what I'm trying to do:

这就是我想要做的:

for (var item in response.data)
{
console.log(item.name);
}

This doesn't work. What went wrong?

这不起作用。什么地方出了错?

Thank you

I GOT IT to work with the following after reading the comment:

在阅读评论后,我得以使用以下内容:

for (var item in response.data){ console.log(response.data[item].name); } })

for(response.data中的var项目){console.log(response.data [item] .name); }})

I was able to get a list of names...

我能够得到一份名单......

Can someone dissect the response as to why it worked?

有人可以解析它为何起作用的回应吗?

3 个解决方案

#1


0  

Check out: Why is using "for...in" with array iteration a bad idea?

退房:为什么在数组迭代中使用“for ... in”是一个坏主意?

For...in iterates through names of the properties of an object. Array items are also considered "properties", so for..in iterates through indexes (which are 0, 1 in your case). As expected when you use response.data[0] you get first element of your array.

For ... in迭代对象属性的名称。数组项也被视为“属性”,因此for..in迭代索引(在您的情况下为0,1)。正如预期的那样,当您使用response.data [0]时,您将获得数组的第一个元素。

#2


10  

data is actually an array (denoted by []), rather than an object so you want a regular for loop rather than a for in.

data实际上是一个数组(用[]表示),而不是一个对象,所以你想要一个常规的for循环而不是for。

for (var i = 0; i<response.data.length; i++) {
  // use i as an array index
  console.log(response.data[i].name);
}

In JavaScript, the for in construct is used for iterating over object properties, but to iterate an array an incremental for loop is typically used.

在JavaScript中,for in构造用于迭代对象属性,但是为了迭代数组,通常使用增量for循环。

#3


0  

for..in iterates over the enumerable properties of an object in no particular order (you may get a different order in different browsers). An array is just a plain object with a special length method and handy methods inherited from Array.prototype (some of which depend on the special length property). There is no restriction on what can be used for a property name, they are not restricted to non-negative integers (noting that where a properly name is not a valid identifier, square bracket notation must be used to access its value).

for..in以无特定顺序迭代对象的可枚举属性(您可能在不同的浏览器中获得不同的顺序)。数组只是一个普通对象,具有特殊长度方法和从Array.prototype继承的方便方法(其中一些取决于特殊长度属性)。对于可以用于属性名称的内容没有限制,它们不限于非负整数(注意,如果正确的名称不是有效的标识符,则必须使用方括号表示法来访问其值)。

The indexes of an array are just string property names (i.e. they are just plain object property names) that are non-negative integers, so a for..in loop will iterate over the numeric indexes (again, not necessarily in ascending or descending order) as well as all other enumerable properties, including those on the [[Prototype]] chain. Therefore it is always recommended to include a hasOwnProperty test with for..in unless you want to include inherited enumerable properties.

数组的索引只是字符串属性名称(即它们只是普通对象属性名称),它们是非负整数,因此for..in循环将遍历数字索引(同样,不一定按升序或降序排列) )以及所有其他可枚举属性,包括[[Prototype]]链上的属性。因此,除非要包含继承的可枚举属性,否则始终建议在for..in中包含hasOwnProperty测试。

Because of the above, it is generally much better to iterate over array properties using a counter from 0 to array.length - 1 (since the length is always one bigger than the last index).

由于上述原因,使用从0到array.length - 1的计数器迭代数组属性通常要好得多(因为长度总是比最后一个索引大一个)。

To test the "no particular order" statement, try the following in IE and other browser and note the different order:

要测试“无特定顺序”语句,请在IE和其他浏览器中尝试以下操作并注意不同的顺序:

var a = [];
a[2] = 2;
a[0] = 0;
a[3] = 3;
var b = [];
for (var i in a) b.push(a[i]);
alert(b);

#1


0  

Check out: Why is using "for...in" with array iteration a bad idea?

退房:为什么在数组迭代中使用“for ... in”是一个坏主意?

For...in iterates through names of the properties of an object. Array items are also considered "properties", so for..in iterates through indexes (which are 0, 1 in your case). As expected when you use response.data[0] you get first element of your array.

For ... in迭代对象属性的名称。数组项也被视为“属性”,因此for..in迭代索引(在您的情况下为0,1)。正如预期的那样,当您使用response.data [0]时,您将获得数组的第一个元素。

#2


10  

data is actually an array (denoted by []), rather than an object so you want a regular for loop rather than a for in.

data实际上是一个数组(用[]表示),而不是一个对象,所以你想要一个常规的for循环而不是for。

for (var i = 0; i<response.data.length; i++) {
  // use i as an array index
  console.log(response.data[i].name);
}

In JavaScript, the for in construct is used for iterating over object properties, but to iterate an array an incremental for loop is typically used.

在JavaScript中,for in构造用于迭代对象属性,但是为了迭代数组,通常使用增量for循环。

#3


0  

for..in iterates over the enumerable properties of an object in no particular order (you may get a different order in different browsers). An array is just a plain object with a special length method and handy methods inherited from Array.prototype (some of which depend on the special length property). There is no restriction on what can be used for a property name, they are not restricted to non-negative integers (noting that where a properly name is not a valid identifier, square bracket notation must be used to access its value).

for..in以无特定顺序迭代对象的可枚举属性(您可能在不同的浏览器中获得不同的顺序)。数组只是一个普通对象,具有特殊长度方法和从Array.prototype继承的方便方法(其中一些取决于特殊长度属性)。对于可以用于属性名称的内容没有限制,它们不限于非负整数(注意,如果正确的名称不是有效的标识符,则必须使用方括号表示法来访问其值)。

The indexes of an array are just string property names (i.e. they are just plain object property names) that are non-negative integers, so a for..in loop will iterate over the numeric indexes (again, not necessarily in ascending or descending order) as well as all other enumerable properties, including those on the [[Prototype]] chain. Therefore it is always recommended to include a hasOwnProperty test with for..in unless you want to include inherited enumerable properties.

数组的索引只是字符串属性名称(即它们只是普通对象属性名称),它们是非负整数,因此for..in循环将遍历数字索引(同样,不一定按升序或降序排列) )以及所有其他可枚举属性,包括[[Prototype]]链上的属性。因此,除非要包含继承的可枚举属性,否则始终建议在for..in中包含hasOwnProperty测试。

Because of the above, it is generally much better to iterate over array properties using a counter from 0 to array.length - 1 (since the length is always one bigger than the last index).

由于上述原因,使用从0到array.length - 1的计数器迭代数组属性通常要好得多(因为长度总是比最后一个索引大一个)。

To test the "no particular order" statement, try the following in IE and other browser and note the different order:

要测试“无特定顺序”语句,请在IE和其他浏览器中尝试以下操作并注意不同的顺序:

var a = [];
a[2] = 2;
a[0] = 0;
a[3] = 3;
var b = [];
for (var i in a) b.push(a[i]);
alert(b);