问题描述
我有一个Web服务,该服务返回JSON编码的数据数组.然后,我使用jQuery的.each()
函数遍历该数组,但是在Firefox中,它向下迭代,而在Chrome中,它向上迭代.
I have a web service that returns a JSON encoded array of data. I then use jQuery's .each()
function to iterate through that array but in Firefox it iterates down while in Chrome it iterates up.
从Web服务获取的数据是:
the data that come sback from the web service is:
{
"data": {
"610": {
"id": "610",
"url": "a url 1",
"description": "XXX YYY",
"toc": "0000-01-00",
"active": "1"
},
"608": {
"id": "608",
"url": "a url 1",
"description": "ytttgffrr",
"toc": "0000-01-00",
"active": "1"
},
"607": {
"id": "607",
"url": "a url 3",
"description": "rtretert3",
"toc": "0000-01-00",
"active": "1"
},
"606": {
"id": "606",
"url": "a url 4",
"description": "xxxwwww",
"toc": "0000-01-00",
"active": "1"
},
...
}
}
Firefox运行610-> 606,而chrome fors 606-> 610.
Firefox goes 610 -> 606 while chrome fors 606 -> 610.
有什么想法,为什么我可以做什么?
Any ideas why and what I can do about it?
推荐答案
对象中的属性没有固有顺序. ECMAScript规范不保证for…in
语句将按其属性按字母顺序遍历对象. jQuery.each
在对象上使用时在内部使用for…in
.
Properties in objects have no inherent order. The ECMAScript specification doesn’t guarantee that for…in
statements will traverse the object by its properties in alphabetical order. jQuery.each
uses for…in
under the hood when used on objects.
如果顺序对您很重要,请使用数组而不是对象.
If the order is important to you, use an array instead of an object.
这篇关于在Firefox和Chrome中更改.each()的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!