Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare hashmap in javascript with <String,String array>

I want to declare a hashmap in javascript with <String, String array> instead of <String,Integer>. How can that be done ?

like image 262
user782400 Avatar asked Apr 02 '26 17:04

user782400


1 Answers

If you plan to use a javascript Array object, be aware that an array index can only be accessed via integers.

var arr = [];
arr['person'] = 'John Smith';

alert(arr.length); // returns 0, not an array anymore;

and

var arr = [];
arr[0] = 'John Smith';

alert(arr.length); // returns 1, still an array;

The above would work in javascript, but var arr actually is not an array object anymore. You cannot sort it, for example.

So for you hashmap you could do

var map = new Object();

map['person'] = [];
map['person']['test'] = 'myvalue';
map['person']['test2'] = 'myvalue2';

alert(map['person']['test']);
like image 199
Chris Avatar answered Apr 04 '26 06:04

Chris