• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Map in Javascript

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67590
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this post is really helpful. thanks everybody.
reply
    Bookmark Topic Watch Topic
  • New Topic