By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,605 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,605 IT Pros & Developers. It's quick & easy.

problems with associative arrays

P: n/a
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

Sep 30 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 30 Sep 2005 00:33:38 -0700, nk******@gmail.com wrote:
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.

--
~ Io non soffro di pazzia, ne godo ogni minuto.
(I don't suffer from insanity, I enjoy every minute of it)

Sep 30 '05 #2

P: n/a
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']

Sep 30 '05 #3

P: n/a
nk******@gmail.com wrote:
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?


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/ec127472a6d9f417/f59d2969df16c937?q=array+length+VK&rnum=1&hl=en#f5 9d2969df16c937>

--
Rob
Sep 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.