JavaScript关联数组没有订购? [英] JavaScript associative arrays not ordered?

查看:54
本文介绍了JavaScript关联数组没有订购?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我不能流利使用JavaScript,所以我可能会忽略这一点。


但是在所有其他编程语言中我知道并且

有关联数组或哈希值,

哈希中的元素按字母顺序排序,如果密钥恰好是

be字母数字。我认为这是有道理的,因为

它允许快速查找密钥。


但在JavaScript中,我发现这不是真的。


考虑以下函数:


函数print_assoc_array(){

var assoc_array = new Object();


assoc_array [" one"] = 1;

assoc_array [" two"] = 2;

assoc_array [" three" ;] = 3;

assoc_array [" four"] = 4;

assoc_array [" five"] = 5;


document.open();

for(i in assoc_array){

document.writeln(i +" =" + assoc_array [i] +" ;< br>");

}

document.close();

}


它以插入

顺序打印关联数组的内容,而不是按字母顺序打印(IE6和FF1.5.0.6)。


Am我错过了什么,或者这是预期的?

Rene

-

Rene Nyffenegger
http://www.adp-gmbh.ch /

解决方案

Rene Nyffenegger< re ************** @ gmx.chwrites:


我不能流利使用JavaScript,所以我可能会忽略显而易见的事情。


但在所有其他编程语言中我都是知道并且

有关联数组或散列,

散列中的元素按字母顺序排序,如果密钥恰好是

是字母数字。



嗯,你知道的语言不同于我。我已经习惯了Java'的
HashMap,它根本没有排序。


我相信这是有道理的,因为它允许快速查找

键。



仅当您依赖可排序的密钥时,这对于您可以用作密钥的内容有严格的限制。


使用排序键和二进制搜索实现关联数组

使得添加元素变得昂贵,并且仍然需要对数的b / b
时间进行查找。使用基于散列的实现已经(摊销)

常量时间添加和查找。


但是在JavaScript中,我发现这不是真的。



没有什么要求它是真的。在某些浏览器中可能是这样,但是

.....


考虑以下功能:

函数print_assoc_array(){



....


}


它以插入

顺序打印关联数组的内容,而不是按字母顺序打印(IE6和FF1.5.0.6)。



....这是IE和Netscape的传统行为,其他浏览器模仿了
(Opera曾试图忽略它,因为记住

插入顺序为基于哈希的实现增加了更多的工作,但是用户要求他们模仿IE浏览器。


我错过了什么,或者这是预期的?



这是传统的预期。我希望订单上没有

的预期,因为插入订单非常随意,

但我想我已经太迟了页面出来

取决于当前的行为。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http:// www。 infimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''


*** Rene Nyffeneggerescribió/写道(星期六,2006年8月19日08:59:49 +0000

(UTC)):


但是在所有其他我知道的编程语言和

有关联数组或散列,

哈希中的元素按字母顺序排序,如果密钥恰好是
是字母数字。我相信这是有道理的,因为

它允许快速查找密钥。



外部表示与内部

实现无关。当你创建一个目录列表并获得按

名称排序的文件时,这并不意味着文件在磁盘上物理排序并且操作系统移动

每个磁盘周围的文件你创建或重命名文件的时间。


在我所知道的所有语言中(我承认并不多),关联数组

维持插入顺序。这是有道理的:否则,他们不会像信息存储那么有用。排序是一种消耗

操作的资源,无法撤消。没有为
性能创建关联数组。


此外,在这个确切的情况下,你不是真的有一个数组而是一个对象。 。

JavaScript只定义一维数字数组;其他类型的数组

必须用对象模拟。按字母顺序排序对象

属性没有意义。

-

- + http://alvaro.es - álvaroG。Vicario - 西班牙布尔戈斯

++ Mi sitiosobreprogramaciónweb : http://bits.demogracia.com

+ - Mi web de humor con rayos UVA: http://www.demogracia.com
-


Rene Nyffenegger写道:


我不会精通JavaScript,所以我可能会忽略显而易见的事情。


但是在我所知道的所有其他编程语言中,

都有关联数组或哈希,是字母数字,则
hash按字母顺序排序。我认为这是有道理的,因为

它允许快速查找密钥。


但在JavaScript中,我发现这不是真的。


考虑以下函数:


函数print_assoc_array(){

var assoc_array = new Object();

assoc_array [" one"] = 1;

assoc_array [" two"] = 2;

assoc_array [" three"] = 3 ;

assoc_array [" four"] = 4;

assoc_array [" five"] = 5;

document.open() ;

for(i in assoc_array){

document.writeln(i +" =" + assoc_array [i] +"< br>") ;

}

document.close();

}


打印内容插入中的关联数组

顺序而不是按字母顺序排列(IE6和FF1.5.0.6)。



我不知道你是否可以改变输入格式;但是做了


new numero(one,1)

new numero(" two"," 2")

new numero(" three"," 3")

应该为您提供排序的所有可能性。请参阅以下示例:
http:// groups.google.com/group/comp....918860bbb62a84

-

巴特


Hello everyone.

I am not fluent in JavaScript, so I might overlook the obvious.

But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric. Which I believe makes sense because
it allows for fast lookup of a key.

But in JavaScript, I found this not to be true.

Consider the following function:

function print_assoc_array() {
var assoc_array = new Object();

assoc_array[ "one"] = 1;
assoc_array[ "two"] = 2;
assoc_array["three"] = 3;
assoc_array[ "four"] = 4;
assoc_array[ "five"] = 5;

document.open();
for (i in assoc_array) {
document.writeln(i + " = " + assoc_array[i] + "<br>");
}
document.close();
}

It prints the content of the associative array in insertion
order rather than in alphabetical order (IE6 and FF1.5.0.6).

Am I missing something, or is this expected?
Rene
--
Rene Nyffenegger
http://www.adp-gmbh.ch/

解决方案

Rene Nyffenegger <re**************@gmx.chwrites:

I am not fluent in JavaScript, so I might overlook the obvious.

But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric.

Well, you know different languages than me. I''m used to Java''s
HashMap, which is not sorted at all.

Which I believe makes sense because it allows for fast lookup of a
key.

Only if you depend on the key being sortable, which is a serious
restriction on what you can use as keys.

Implementing an associative array using sorted keys and binary search
makes it expensive to add elements, and still requires logarithmic
time for lookups. Using a hash based implementation has (amortized)
constant time addition and lookup.

But in JavaScript, I found this not to be true.

Nothing requires it to be true. It could be true in some browsers, but
.....

Consider the following function:

function print_assoc_array() {

....

}

It prints the content of the associative array in insertion
order rather than in alphabetical order (IE6 and FF1.5.0.6).

.... that is the traditional behavior of both IE and Netscape, which
other browsers mimic (Opera once tried to ignore it, since remembering
insertion order adds more work to a hash based implementation, but
users demanded that they mimic IE on this)

Am I missing something, or is this expected?

It''s expected from tradition. I would prefer that there was no
expectations on the order, since insertion order is pretty arbitrary,
but I guess I''m too late for changing that when there are pages out
there depending on the current behavior.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


*** Rene Nyffenegger escribió/wrote (Sat, 19 Aug 2006 08:59:49 +0000
(UTC)):

But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric. Which I believe makes sense because
it allows for fast lookup of a key.

The external representation has nothing to do with the internal
implementation. When you make a directory listing and get files sorted by
name, it doesn''t mean that files are physically sorted on disk and OS moves
files around the disk every time you create or rename a file.

In all languages I know (not many, I admit), associative arrays
maintain the insertion order. Which makes sense: otherwise, they wouldn''t
be so useful as information storage. Sorting is a resource consuming
operation which cannot be undone. Associative arrays were not created for
performance.

Also, in this exact case, you don''t really have an array but an "Object".
JavaScript only defines one-dimension numeric arrays; other sort of arrays
must be simulated with objects. And there''s no point in sorting object
attributes alphabetically.
--
-+ http://alvaro.es - álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--


Rene Nyffenegger wrote:

I am not fluent in JavaScript, so I might overlook the obvious.

But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric. Which I believe makes sense because
it allows for fast lookup of a key.

But in JavaScript, I found this not to be true.

Consider the following function:

function print_assoc_array() {
var assoc_array = new Object();
assoc_array[ "one"] = 1;
assoc_array[ "two"] = 2;
assoc_array["three"] = 3;
assoc_array[ "four"] = 4;
assoc_array[ "five"] = 5;
document.open();
for (i in assoc_array) {
document.writeln(i + " = " + assoc_array[i] + "<br>");
}
document.close();
}

It prints the content of the associative array in insertion
order rather than in alphabetical order (IE6 and FF1.5.0.6).

I don''t know if you can alter the input format; but doing

new numero("one", "1")
new numero("two", "2")
new numero("three", "3")

should leave you all possibilities to sort. See example on:
http://groups.google.com/group/comp....918860bbb62a84

--
Bart


这篇关于JavaScript关联数组没有订购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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