How can you create the JavaScript/JQuery equivalent of this Java code:
Map map = new HashMap(); //Doesn't not have to be a hash map, any key/value map is fine
map.put(myKey1, myObj1);
map.put(myKey2, myObj2); //Repeat n times
function Object get(k) {
return map.get(k);
}
Source: Tips4all
var map = new Object();
ReplyDeletemap[myKey1] = myObj1;
map[myKey2] = myObj2;
function get(k) {
return map[k];
}
//map[myKey1] == get(myKey1);
Just use plain objects:
ReplyDeletevar map = {"key1":"value1","key2":"value2"}
function get(k){
return map[k];
}
var map = {'myKey1':myObj1, 'mykey2':myObj2};
ReplyDelete// You don't need any get function
// just use
// map[[mykey1]
If you're not restricted to JQuery, you can use the prototype.js framework. It has a class called Hash: You can even use JQuery & prototype.js together. Just type jQuery.noConflict();
ReplyDeletevar h = new Hash();
h.set("key", "value");
h.get("key");
h.keys(); // returns an array of keys
h.values(); // returns an array of values
var oa_data = new java.util.HashMap();
ReplyDelete//Repeat as many times as required to the key/value pairs
oa_data.put("MasterID","aabbcc");
works for me