Using a map

Using a map

Hi there,

Not sure if this a JQuery question or a javascript one. Let's see it. I have a web application where initially it is loaded a map with ids and names. Later, periodiocally a json containing the userId and a message is processed. Initially, I tried to define the map like this:

  1. var ids = [ {${user.id} : '${user.name}} ];

This is loaded within a loop in jsp, so we got something like this:
  1. var ids = [ {34:'user1'}, {500:'user2'}]

The problem with this declararion is that for getting an id when the json is being processed, we don't know any way but to iterate trought all the map, that it is not very efficient.

We have also tried to use an array instead of a map, something like this:
  1. var ids = new Array();
  2. ids[34] = 'user1';
  3. ids[500] = 'user2';
This works well but the problem is the resulting array length is too big, depending on the bigger id it may be in the thousend order.

So I would like to know whether there is a efficient way to obtain the id using a map.

Another question I would like to solve is how using javascript it is possible to define a map with dinamic values. It is easier with an example:
       
  1. var name = $("#name").val();
  2. var value = $("#value").val();
  3. var map = [ {name : value} ];
The problem is that instead of the map has the value of the variable name, has the 'name' label. Is it possible to get a map with the value of a variable???

Thanks in advance!