键值对的顺序(“关联数组”) [英] Order of Key-Value pair ("associative arrays")

查看:136
本文介绍了键值对的顺序(“关联数组”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,


可以依赖插入关联Javascript

数组的键的顺序吗?例如:

var o = new Object();


o [" first"] =" Adam";

o [" second"] =" Eve";

o [" third"] =" Cane";

o [" fourth"] =" Abel;


..

..

..


/ / /


for(var i in o){

alert(i);

}


这是for循环中的顺序*保证*为:

第一,第二,第三,第四?


另外,如果是第三,密钥被删除,订单是否仍然有保证?


我已经在IE和Firefox中对此进行了测试,它似乎有效。我将不胜感激

用于在Javascript标准/规格中表明这一点的任何链接。


非常感谢&亲切的问候,


Abdullah

Hi folks,

Can one rely on the order of keys inserted into an associative Javascript
array? For example:
var o = new Object();

o["first"] = "Adam";
o["second"] = "Eve";
o["third"] = "Cane";
o["fourth"] = "Abel";

..
..
..

//then

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

Is the order in this "for loop" *guaranteed* to be:
first, second, third, fourth?

Also, if the "third" key is deleted, is the order still guaranteed?

I''ve tested this in IE and Firefox, and it seems to work. I''d be grateful
for any links that indicate this in Javascript standards/specs.

Many thanks & kind regards,

Abdullah

推荐答案

2004年5月25日星期二12:35:50 +0200 ,Abdullah Kauchali写道:


[cut]
On Tue, 25 May 2004 12:35:50 +0200, Abdullah Kauchali wrote:

[cut]
这个中的顺序是for循环吗? *保证*为:
第一,第二,第三,第四?


不幸的是,不是。

取决于浏览器对ECMAScript这方面的实现

规格。


在IE,旧的Netscape版本和基于Gecko的浏览器中,订单是

" chronological" ..但是......试试Opera。

表示在Javascript标准/规范中表明这一点的任何链接。
Is the order in this "for loop" *guaranteed* to be:
first, second, third, fourth?
Unfortunately, not.
Depend of the browser''s implementation of this aspect of ECMAScript
specifications.

In IE,old Netscape versions, and Gecko based browsers, the order is
"chronological".. But.. try with Opera.
for any links that indicate this in Javascript standards/specs.



http://www.ecma-international.org/pu...T/Ecma-262.pdf


[for-in statement]

"枚举属性(..)的机制是

依赖于实现。枚举的顺序由

对象定义。 (..)"


-

ZER0://coder.gfxer.web-designer/


~Grom devo regolare la stampante laser per stordire?

(如何设置激光打印机眩晕?)


播出~~ Linkin Park - Somewhere I Belong(真实歌曲)"



http://www.ecma-international.org/pu...T/Ecma-262.pdf

[for-in statement]
"The mechanics of enumerating the properties (..) is
implementation dependent. The order of enumeration is defined by the
object. (..) "

--
ZER0://coder.gfxer.web-designer/

~ Come devo regolare la stampante laser per stordire?
(How do I set a laser printer to stun?)

on air ~ "Linkin Park - Somewhere I Belong (real song)"




" ZER0" <泽******** @ libero.it>在消息中写道

"ZER0" <ze********@libero.it> wrote in message
http://www.ecma-international.org/pu...T/Ecma-262.pdf

[for-in statement]
"枚举属性(..)的机制取决于实现。枚举的顺序由
对象定义。 (..)"
http://www.ecma-international.org/pu...T/Ecma-262.pdf

[for-in statement]
"The mechanics of enumerating the properties (..) is
implementation dependent. The order of enumeration is defined by the
object. (..) "



何笨蛋! :(


感谢零。


现在,如果订单无法保证,我该如何完成以下任务:


1.将对象放在集合中;

2.根据它们放入
$ b $的顺序访问每个对象b集合。


到目前为止,我有这个:


在我看来,最直接的答案是创建

另一个集合,索引编号从0开始,然后总是

递增以添加到集合中。一旦索引/偏移编号

使用,它不能重复使用。


因此,经过一些添加和删除后,我会得到类似的结果:


1, 2,3,7,9,11,23


如果我想加入这个系列,下一个将是24,依此类推。


这是最优雅的解决方案吗?


亲切的问候


Abdullah


Ho bummer! :(

Thanks for that Zero.

Now, how do I accomplish the following if the order is not guaranteed:

1. Put objects within the "collection";
2. Access each one according to the order in which they were put into the
collection.

So far, I have this:

It seems to me that the most straight-forward answer would be to create
another "collection" with index numbers beginning with 0 and then always
incrementing for additions to the collections. Once a index/offset number
is used, it cannot be reused.

So, after some additions and deletions, I''ll get something like:

1, 2, 3, 7, 9, 11, 23

if I wanted to add to this collection, the next one would be 24 and so on.

Is that the most elegant solution?

Kind regards

Abdullah


>可以依赖插入关联Javascript
> Can one rely on the order of keys inserted into an associative Javascript
数组的键的顺序吗?例如:

var o = new Object();

o [" first"] =" Adam";
o [" second" ] =Eve;
o [第三] =Cane;
o ["第四个] =Abel;
array? For example:

var o = new Object();

o["first"] = "Adam";
o["second"] = "Eve";
o["third"] = "Cane";
o["fourth"] = "Abel";




编号ECMAScript规范允许对象的内容无序地为
。大多数实现都会对内容进行排序,但你应该不依赖于它。


此外,初始化对象的首选方法是使用对象

字面符号。


var o = {first:" Adam",second:" Eve",third:" Cain",

第四名:Abel};


参见 http://www.JSON.org


这篇关于键值对的顺序(“关联数组”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆