[英]Does a sparse Javascript array with whole number keys remain sorted by key?
For example: 例如:
var a = [];
a[5] = 'five';
a[2] = 'two';
a[11] = 'eleven';
a[210] = 'two-ten';
a[4] = 'four';
a[56] = 'fifty-six';
a[1] = 'one';
a[39] = 'thirty-nine';
for( var i in a ) {
if( a.hasOwnProperty(i) ) {
console.log(i+":"+a[i]);
}
}
Results in the following (for a few browsers I have handy to test.) 结果如下(对于一些我方便测试的浏览器。)
1:one
2:two
4:four
5:five
11:eleven
39:thirty-nine
56:fifty-six
210:two-ten
Is this standard, reliable behaviour? 这是标准的,可靠的行为吗?
They will always be in order, yes, but standards (as far as I'm aware) end up setting all of the in-between indexes as undefined. 它们总是按顺序排列,是的,但标准(据我所知)最终将所有中间索引设置为未定义。
Edit: If you initialize it as an object, with curly braces, instead of square braces (for arrays) you may get irregular behaviors between browsers. 编辑:如果您将其初始化为对象,使用花括号,而不是方括号(对于数组),您可能会在浏览器之间获得不规则的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.