File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes javascript HashMap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "javascript HashMap?" Watch "javascript HashMap?" New topic
Author

javascript HashMap?

Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
Folks,
Is there a HashMap object in Javascript like the HashMap of Java? If yes is there a way to copy efficiently the Java HashMap to Javascript HashMap.
[ March 08, 2006: Message edited by: Bear Bibeault ]

Thanks,
Santosh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

There isn't one per se, but since you can assign properties to an object on-the-fly in Javascript, the Object itself can act as an ad-hoc map.

Consider:



This may not be a "hash" map per se since we have no way of knowing what algorithm is being used to store and retrieve the properties, but for most intents, it looks and acts just like a map.

You could also use the "dot" notation:



Your second question regarding "converting" a server-side Java Map to a client-side Javascript construct is more of a JSP question, so now that the above is settled, I've moved this to the JSP forum.
[ March 08, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
BB,
With what you have mentioned can I assign Arrays instead of 1,2,3.... as Values of Key's?


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

Absolutely... there's no magic here. All we are doing is using the dynamic nature of Javascript object properties to simulate a Map-like structure. The values of the properties are just like any other Javascript property because that's all that it is!
[ March 08, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

e.g.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60045
    
  65

As to the "conversion" question:

I'd place the Java Map on the request in a servlet controller, and after forwarding to the JSP, use the JSTL <c:forEach> tag to iterate over the Map entries in order to emit the Javascript statements necessary to build the corresponding Javascript structure at page-load.
Santosh Ramachandrula
Ranch Hand

Joined: Apr 04, 2004
Posts: 252
BB thanks for your ideas and quick reply
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javascript HashMap?
 
Similar Threads
html:checkbox probs
Using IFRAMES
static content access
validwhen validation rule
Accessing Java objects in Javascript.