aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Map in Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Map in Javascript" Watch "Map in Javascript" New topic
Author

Map in Javascript

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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] ???

Thanks
Mark
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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 ]
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Ah right i see, Thanks Bear Bibeault, Sure ill investigate further and hopefully come up with a solution.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Solution? Are you still having an issue?
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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

using


Thanks
Mark
[ November 23, 2007: Message edited by: Mark Hughes ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 123

this post is really helpful. thanks everybody.


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map in Javascript