Forums Register Login
Map in Javascript
Hey Guys,
Sorry i have perhaps silly question for ye, Im newish to javascript and am wondering does the map variable have a method associated with it to get the value of a key much like the HashMap object in java.
I was told a map can be declared as

and you can put information it it like

So im wondering is there away to print out the value of the key like output.getvalue[key] ???

(1 like)

Originally posted by Mark Hughes:
I was told a map can be declared as

Sort of. That just creates an empty instance of a JavaScript Object. It's identical to:

There really isn't any implementation of Map in JavaScript.

But... JavaScript objects can be assigned properties on the fly, so an Object acts a lot like a map.

For example, after declaring your variable as shown above, you could write:

and now the object has a property named abc that contains the value 123.

The value can be retrieved with either of:

which are completely equivalent.

So in short, there is no Map in JavaScript, but you can use a JavaScript Object instance in a manner very similar to what we think of as a Map in Java.
[ November 22, 2007: Message edited by: Bear Bibeault ]
Analyzing your example:

This creates a property on output named testKey:KEY.

That's a bit weird. It's not customary to create properties that contain colons. In fact, even though you can, I'd strongly advise against it -- too many opportunities to screw things up and it looks odd and confusing. Generally, properties follow the naming conventions for variables (think member variables in Java).

The value of the property is a JavaScript object that itself contains a property named testKey that has a value of 'value'.
[ November 22, 2007: Message edited by: Bear Bibeault ]
Ah right i see, Thanks Bear Bibeault, Sure ill investigate further and hopefully come up with a solution.

Originally posted by Mark Hughes:
ill investigate further and hopefully come up with a solution.

Solution? Are you still having an issue?
Hi again,
No im good thanks, looking at your examples for a while got me thinking on the right track. I could ouput the value of testValue


[ November 23, 2007: Message edited by: Mark Hughes ]
Or even:

The general [] notation is usually reserved for when the property name is in another variable, or when the property namer contains non-identifier characters.
this post is really helpful. thanks everybody.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 254433 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 20, 2018 10:54:47.