Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map in Javascript

 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah right i see, Thanks Bear Bibeault, Sure ill investigate further and hopefully come up with a solution.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65104
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this post is really helpful. thanks everybody.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic