I want to create an object starting with something like:
var map = {};
Then I want to add elements using this function:
add = function(integerA, objectB) { map[objectB.type][integerA] = objectB; }
So this is a random example of the structure of an object that I want to achieve:
map = { 'SomeType' : { 0 : 'obj', 2 : 'obj', 3 : 'obj' }, 'OtherType' : { 0 : 'obj', 5 : 'obj' }, };
Now, my problem. I can not do map[objectB.type][integerA] = objectB; because map[objectB.type] is undefined. I could solve this by checking if map[objectB.type] through an if-statement and if necessary creates map[objectB.type] = {}; .
Otherwise, I could preload all types of objects. However, I would prefer not to.
My question is: is there a way to create an object on the fly without checking if there is a type that exists every time I want to call the add function or preload all types?
It is important that my add function is as fast as possible, and that the map object is correct, because I need to read and write a lot in a short amount of time (this is an animation / game application).
javascript object multidimensional-array
user8363
source share