473,761 Members | 7,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript associative arrays not ordered?

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_arr ay() {
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.writel n(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/
Aug 19 '06
41 4973
Richard Cornford wrote:
(That is, if you want to allow
pre-defined/pre-existing key/value pairs, as you must if the
javascript object is to be included in the category,
You needn't allow nor disallow pre-existing key/value pairs.
the keys for
those properties should be included in a list of "all keys" or it is
not a list of all of the keys.)
Replace "all keys" with "all user-defined keys" then, if you want to be
argumentative.

That leaves my definition from the earlier post (which you did not respond
to) as:

"An associative array is an unordered collection of values/objects which
allows for the storage and retrieval of a value/object [Value] using a
user-defined value/object [Key] as the only storage/retrieval criteria such
that retrieving a Value stored for a given Key will return the Value
previously stored for the given Key, and also supplying a method to retrieve
an unordered collection of all user-defined Keys."

Objections?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 24 '06 #41
Matt Kruse wrote:
Richard Cornford wrote:
>(That is, if you want to allow
pre-defined/pre-existing key/value pairs, as you must if the
javascript object is to be included in the category,

You needn't allow nor disallow pre-existing key/value pairs.
You need to either not disallow them or to exclude the javascript
object from being an associative array. Not disallowing is allowing.
>the keys for
those properties should be included in a list of "all keys" or it is
not a list of all of the keys.)

Replace "all keys" with "all user-defined keys" then, if you want to
be argumentative.
And in making the terms less precise you risk reducing the definition
to a point where it is so generally applicable as to be saying nothing
useful.
That leaves my definition from the earlier post (which you did not
respond to) as:
Your news server sounds like it has some catching up to do.
"An associative array is an unordered collection of values/objects
which allows for the storage and retrieval of a value/object [Value]
using a user-defined value/object [Key] as the only storage/retrieval
criteria such that retrieving a Value stored for a given Key will return
the Value previously stored for the given Key, and also supplying a
method to retrieve an unordered collection of all user-defined Keys."

Objections?
Personally I would not consider the ability to list all the keys as
fundamental to the concept of an associative array and so would not
like to see it in any definition. I also would not be too concerned to
require the object to know how many keys are in use (no matter how
common that may be in actual associative arrays). These are getting
towards the language specific aspects of the question that were warned
about yesterday.

However, it is not a question of objecting to any given definition
-presented as such, but instead seeing how well it discriminates
objects that objectively are associative arrays from those that are
not, and whether it puts the javascript object on the associative array
side of that demarcation.

Again the vagary in "user-defined value/object [Key]" leaves the actual
nature of the keys completely open, and so includes many objects that
are not associative arrays. However, as the object that is to be
considered an associative array by this definition must provide a
method to "retrieve an unordered collection of all user-defined Keys"
the keys that could be returned from such a method act to define what
is a "user-defined" key.

The javascript object only provides one method that could be this
method to "retrieve an unordered collection of all user-defined Keys",
and that is the - for-in - loop. Thus a "user-defined" key becomes any
property name that once added to the object is then enumerated by a
for-in loop (how well that for-in satisfies your demand that it be
unordered is not important here). Thus the javascript object is
categorised as not being an associative array by your definition as
there are keys that will be enumerated following an attempt to use them
for assignment of a value to a new property of the object that cannot
be used to retrieve the value assigned during that assignment ("such
that retrieving a Value stored for a given Key will return the Value
previously stored for the given Key" is false for keys that will be
enumerated by for-in and so need to qualify as "user-defined" in order
to satisfy "also supplying a method to retrieve an unordered collection
of all user-defined Keys").

(We also get to quibble again about how ordered/unordered examples of
actual associative arrays may be, and how ordered/unordered the
javascript object actually is, but that is not important here as the
javascript object is already excluded by this definition).

Richard.

Aug 24 '06 #42

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
11205
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
35
6666
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
104
17002
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.