title: Javascript: multi-dimensional object
body: <p>I want to create an object, starting from something like:</p>
  
  <pre><code>var map = {};
  </code></pre>
  
  <p>Then, I want to add items with this function:</p>
  
  <pre><code>add = function(integerA, objectB) {
      map[objectB.type][integerA] = objectB;
  }
  </code></pre>
  
  <p>So, this is a random example of the object structure I want to achieve:</p>
  
  <pre><code>map = {
      'SomeType' : { 0 : 'obj', 2 : 'obj', 3 : 'obj' },
      'OtherType' : { 0 : 'obj', 5 : 'obj' },
  };
  </code></pre>
  
  <p>Now, my problem. I can't do <code>map[objectB.type][integerA] = objectB;</code> because <code>map[objectB.type]</code> is not defined. I could solve this by checking if <code>map[objectB.type]</code> exists through an if-statement and create <code>map[objectB.type] = {};</code> when necessary.</p>
  
  <p>Otherwise I could pre-load all object types. However I would prefer not to have to do this.</p>
  
  <p>My question: is there a way I can create the object 'on the fly' without having to check if the type already exists every time I want to call the <code>add</code> function or to pre-load all the types?</p>
  
  <p>It is important that my add function is so fast as possible and that the map object is correct, because I need to read and write a lot in a small amount of time (it's an animation / game application).</p>