title: JavaScript insert into Array at index
body: <p>I know how to insert into array at index, when that index is already present. Say <code>arr = [1, 2, 3, 4]</code> and <code>arr.splice(2, 0, 0)</code> will result in <code>[1, 2, 0, 3, 4]</code>. However I have an empty array which is getting filled from an object which is not in any particular order. Actually array is used to sort the object:</p>
  
  <pre><code>var key  = parseInt(object[object_key].timestamp);
  while (key &amp;&amp; array_sorted[key]) { //Attempt at preventing overwriting elements with same timestamp
      key++;
  }
  key = key ? key : array_sorted.length + 1;
  array_sorted.splice(key, 0, object[object_key].data);
  </code></pre>
  
  <p>Now this will build an array <code>[object[0].timestamp, object[1].timestamp, object[2].timestamp, ...]</code> instead of something like this <code>[undefined, ..., undefined, object[4].timestamp, object[3].timestamp, undefined, object[0].timestamp, undefined, ..., undefined, object[1].timestamp]</code>... How can I achieve sorting of an object of objects by <code>timestamp</code> (object's object's property) using this method? Is there a way to insert at index of an empty array?</p>