开发者

array in javascript

开发者 https://www.devze.com 2023-01-17 10:58 出处:网络
i have a piece of code like below var abc = { 2: {label: \"raju\"}, 10: {label: \"samu\"}, 3:{label: \"sidh\"},

i have a piece of code like below

var abc = 
{
2: {label: "raju"},
10: {label: "samu"},
3:  {label: "sidh"}, 
1:  {label: "kat"}, 

};

for(var i in abc){ alert(i); }

in mozilla fire fox it alerts 2,10,3,1. but in chrome it shows 1,2,3,10

but my requirement is the first one (as shown in fire 开发者_StackOverflowfox) what to do for getting the same result in chrome?


The order in which the "for(var i in ...)" retrieves the items is not defined. For an array-like behaviour you have to use an array ;)

var abc = [
  { nr: 2, label: "raju" },
  { nr: 10, label: "samu" },
  { nr: 3, label: "sidh" },
  { nr: 1, label: "kat" }
];

for (var i = 0, length = abc.length; i < length; i++)
   alert(abc[i].nr);
0

精彩评论

暂无评论...
验证码 换一张
取 消