One thing you could do is something like this ( 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, this can be done even better, but it makes sense. Personally, I would write a class for this.
Edit: Also keep in mind that this creates an array with high potential, with many undefined values โโthat you need to take care of when reading from it. In addition, arrays with such openings have poor performance (if this is a problem - for several, even several hundred values, it does not matter).
Ingo bรผrk
source share