Re: problems with associative arrays



On 30 Sep 2005 00:33:38 -0700, nkparimi@xxxxxxxxx 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)

.



Relevant Pages

  • Re: What does this script means?
    ... Array objects, therefore there is no special syntax for such objects. ... Where the difference between square bracket notation and dot notation becomes significant is, as you've observed, that identifiers cannot start with digits and underscore symbols). ...
    (comp.lang.javascript)
  • Re: Updated Javascript Best Practices document
    ... name form elements as array objects: ... There are times when Dot Notation simply will not work and Bracket Notation does. ... But for a novice - which that document is intended for - the best general advice is to use Bracket Notation. ...
    (comp.lang.javascript)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Need help with textboxes
    ... The JavaScript 1.5 Reference already states: ... All forms and their children are stored in an array ... use dot notation or object literals. ... No. Bracket property accessors allow their argument to be any string value. ...
    (comp.lang.javascript)
  • Re: Garbage Collection Issues in long-standing services
    ... the pinned array should get unpinned or ... The receive case is done quite well, I do create a 4K buffer and reuse it ... and remove the reference to my wrapper socket class, ...
    (microsoft.public.dotnet.languages.csharp)