Order of iteration differs in IE9

女生的网名这么多〃 提交于 2019-12-02 01:57:55

Property enumeration order is undefined in ECMAScript up to and including version 5 (the current version at time of writing) and does vary between browsers, so you shouldn't rely on any specific ordering. If you need predictable ordering, use an array and a for or while loop. For your example, one option would be:

var arr = [
  {rank: "5", name: "John"},
  {rank: "1", name: "Kumar"},
  {rank: "3", name: "Rajesh"},
  {rank: "2", name: "Yogesh"}
];

for (var i = 0; i < arr.length; ++i) alert(arr[i].rank);

One final note: enumeration order when using a for...in loop is not guaranteed for any kind of object, including arrays, so you should always use for or while when order matters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!