JavaScript - control two-dimensional dynamic array -


i want init two-dimensional dynamic array in javascript, don't limit element (maybe) like

var dynamic = new array (); dynamic[] = new array (); 


after want add value special array like

dynamic[id].push(2); // id = 3, dynamic[3][0] = 2 ... dynamic[id].push(3); // id = 3, dynamic[3][1] = 3 ... dynamic[id].push(5); // id = 5, dynamic[5][0] = 5 

it's possible? how can that, thanks

one thing (jsfiddle):

var dynamic = [];  dynamic.push = function (id, value) {     if (!this[id]) {         this[id] = [];     }      this[id].push(value); }  dynamic.push(3, 2); dynamic.push(3, 3); dynamic.push(5, 5); 

of course, can done better, gets point across. personally, i'd write class this.

edit: also, keep in mind creates array high potential of having whole lot of undefined values, needs taken care of when reading it. also, arrays holes have bad performance (if issue -- few, few hundred, values, won't matter).


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -