473,224 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Associative array

I am curious why some people feel that Javascript doesn't have
associative arrays. I got these definitions of associative arrays via
goggle:

Arrays in which the indices may be numbers or strings, not just
sequential integers in a fixed range.
http://www.sunsite.ualberta.ca/Docum...r/gawk_20.html

(n.) A collection of data (an array) where individual items can be
indexed (accessed) by a string, rather than by an integer as is common
in most programming languages.
docs.sun.com/db/doc/805-4368/6j450e60a

(n.) A collection of data (an array) where individual items may be
indexed (accessed) by a string, rather than by an integer as is common
in most programming languages.
www.npac.syr.edu/nse/hpccgloss/hpccgloss.html

Another name for a dictionary.
academics.tjhsst.edu/compsci/thinkCS/chap19/node11.html
My goggle search:
http://www.google.com/search?hl=en&l...ociative+Array
It seems to me that both javascript arrays and object meet these
definitions.

One problem with Javascript would be the extra indexes automatically
created but you can check for them if you need too.
Maybe I am missing something.

Robert

Jul 23 '05 #1
4 2630
Robert wrote:
I am curious why some people feel that Javascript doesn't have
associative arrays.


Because Javascript, JScript nor ECMAScript have associative arrays.

Pretty good reason don't you think?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
On 9 Dec 2004 15:09:07 -0800, Robert <rc*******@my-deja.com> wrote:
I am curious why some people feel that Javascript doesn't have
associative arrays.
The behaviour has nothing to do with arrays. The misconception arises
because "associative arrays" are introduced thus:

var arr = new Array();

arr['name'] = 'value';

However, you can't apply any Array method to that object and expect it to
act upon 'name' as it's not part of the array; it's part of the object
itself. The same outcome is achieved with:

arr.name = 'value';

The main difference between the two approaches is that with dot notation,
each token must conform to the rules of an identifier. With square bracket
notation, the operand is any valid expression.

I presume that many script authors are unaware that you can use square
brackets to construct property names because the fact that this is what
you're actually doing - accessing object properties - is never discussed.
It's hidden among this illusion.

The fact of the matter is: ECMAScript does not have them. To produce a
robust system of storage you have to write at least *some* supporting
code, which immediately draws attention to the fact that ECMAScript
doesn't provide this functionality natively.

If it helps in some way to think of associative arrays, fine. However,
make sure you know what is really happening behind the scenes.

[snip]
docs.sun.com/db/doc/805-4368/6j450e60a
When posting URLs, please post them in full. Preferably surrounded by
<URL:...>:

<URL:http://docs.sun.com/db/doc/805-4368/6j450e60a>

[snip]
It seems to me that both javascript arrays and object meet these
definitions.
Loosely, perhaps. However, these terms are applied to languages which
feature arrays that can be indexed by strings and numbers. You just aren't
doing that with ECMAScript: an array is *just* an array.
One problem with Javascript would be the extra indexes automatically
created
[Aside]
It's interesting to see you refer to the "extra indexes". Either that's a
Freudian slip, or potential proof of the misconception I mentioned earlier.
[/Aside]
but you can check for them if you need too.


How? You can't enumerate them, and you can never be sure of all of the
properties and methods that will be present. It's best to avoid the issue
entirely by modifying the name you use so that it will never clash with an
existing property.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
"Michael Winter" <M.******@blueyonder.co.invalid> wrote:
The behaviour has nothing to do with arrays. The misconception arises
because "associative arrays" are introduced thus:

var arr = new Array();

arr['name'] = 'value';


Maybe one can show the misconception like this:

var arr = new Array();

(1) arr['name'] = 'value';

Now it looks like one added a key/value pair to a hash.

Array() has methods like pop() or push(), lets give the "hash datatype"
some as well.

(2) arr['keys'] = getkeys; // function defined elsewhere
// The same as arr.keys = getkeys

so now, arr.keys() should get the keys of the hash.

But wait:

arr['foo'] = bar is the way to add a key/value pair.

So (2) should just do this and the hash should contain

'name' = 'value'
'keys' = getkeys

But this is wrong as well.

'keys' should not be a data of the hash. It should be a method like
pop() for accessing the data of a hash.

And yes, it is perfectly OK for a hash to hold functions.
So this is a conflict it the alleged semantics of these
assignment. The reason for the conflict is, that there is no native
hash type in Javascript.

Bye,
Martin
Jul 23 '05 #4
In article <11**********************@z14g2000cwz.googlegroups .com>,
Robert <rc*******@my-deja.com> writes
I am curious why some people feel that Javascript doesn't have
associative arrays.
One reason is that there are two meanings and no-one knows which is
being used. 'Associative array' can describe a very general kind of
storage unit, just as 'stack' can be a very general description.
Alternatively, 'associative array' can be a particular data type
supplied as part of a computer language. You can get some lovely
never-ending arguments if people are using these different meanings.

I got these definitions of associative arrays via
goggle:

Arrays in which the indices may be numbers or strings, not just
sequential integers in a fixed range.
http://www.sunsite.ualberta.ca/Docum...r/gawk_20.html

(n.) A collection of data (an array) where individual items can be
indexed (accessed) by a string, rather than by an integer as is common
in most programming languages.
docs.sun.com/db/doc/805-4368/6j450e60a

(n.) A collection of data (an array) where individual items may be
indexed (accessed) by a string, rather than by an integer as is common
in most programming languages.
www.npac.syr.edu/nse/hpccgloss/hpccgloss.html
Back in the 60s when people were discussing hardware associative arrays
the 'address', or key, or index, was just a bit pattern supplied by the
user. The user might think it was ASCII characters or a floating point
number or whatever, but the hardware couldn't care less.

When an associative array is implemented in software the 'address' has
to be a defined type so the compiler/interpreter knows how to allocate
the right amount of storage when creating a cell, but it still isn't
restricted to integers and strings.

Another name for a dictionary.
academics.tjhsst.edu/compsci/thinkCS/chap19/node11.html
A dictionary is a particular kind of associative array. The 'address' is
a word; the value is a chunk of text.

But then people have used 'dictionary' in a more general way. 'Map' is
another popular name.

My goggle search:
http://www.google.com/search?hl=en&l...ociative+Array
It seems to me that both javascript arrays and object meet these
definitions.

One problem with Javascript would be the extra indexes automatically
created but you can check for them if you need too.
Maybe I am missing something.


True. Both Object objects and Array objects have their good points and
their bad points if your javascript application needs to implement an
associative array.

John
--
John Harris
Jul 23 '05 #5

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

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
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";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
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...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.