Hi,
I'm facing issues with using associative arrays in non-IE browsers. In
particular, I have code like the following:
var IDToAddress = new Array();
IDToAddress['1000000'] = 'apple tree lane, mars';
IDToAddress['2000100'] = 'orchard street, venus';
The keys used in this array are all strings, but each key is actually
just a v. large integer e.g. '1000000'. There are several hundred such
key-value pairs that make up this array.
This works fine in IE, but other browsers simply hang, using up huge
amounts of memory. It appears that not all browsers handle such
associative arrays well - whose keys are large numbers represented as
strings. Has anyone faced similar problems, and are there work-arounds?
thanks
Nagender
ZER0 - 30 Sep 2005 09:09 GMT
> var IDToAddress = new Array();
> IDToAddress['1000000'] = 'apple tree lane, mars';
> IDToAddress['2000100'] = 'orchard street, venus';
Use simply object, not Array:
var IDToAddress=new Object();
IDToAddress['1000000'] = 'apple tree lane, mars';
IDToAddress['2000100'] = 'orchard street, venus';
with object initializer:
ex. 1
var IDToAddress={};
IDToAddress['1000000'] = 'apple tree lane, mars';
IDToAddress['2000100'] = 'orchard street, venus';
ex. 2
var IDToAddress={
'1000000':'apple tree lane, mars',
'2000100':'orchard street, venus'
}
IDToAddress['key']="something";
You can reference the properties with dot notation or squadre bracket
notation:
alert(IDToAddress.key);
alert(IDToAddress["key"]);
This works for all object:
window["alert"]("hello world");
window.alert("hello world");
And you can iterate the properties with for..in statement.
In addition, you can find on net a lot of "classes" for more complex hash
objects.
I hope it helps, and sorry for my english.

Signature
~ Io non soffro di pazzia, ne godo ogni minuto.
(I don't suffer from insanity, I enjoy every minute of it)
Baconbutty - 30 Sep 2005 09:50 GMT
1. All keys are stored as strings, even with a[1], the 1 is stored as
a string.
2. I believe I read in a previous post that for some browsers, if you
set an integer index on an Array, it will fill in every undefined
index position up to it with blanks, whereas IE leaves previous
undefined index positions as undefined.
3. You could also look at putting a non digit prefix in to the key,
e.g. IDToAddress['PREFIX2000100']
RobG - 30 Sep 2005 13:42 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> associative arrays well - whose keys are large numbers represented as
> strings. Has anyone faced similar problems, and are there work-arounds?
You may find this post (and some of the rest of the thread) useful:
<URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ec127472a6
d9f417/f59d2969df16c937?q=array+length+VK&rnum=1&hl=en#f59d2969df16c937>

Signature
Rob