title: How to get the index of an object inside an array of objects?
body: <p>I have a JavaScript array of objects like this:</p>
  
  <pre><code>box[0] = {...}
  box[1] = {...}
  box[2] = {...}
  ...
  box[499] = {...}
  </code></pre>
  
  <p>This objects are generated by the same constructor and added to the array inside a loop. The objects have methods in the prototype which need to know the object's <strong>index</strong> in the array to do their stuff. Currently what I am doing is to set a property called <code>id</code> inside each object when I create it inside the loop, equal to the array index. Something like this:</p>
  
  <pre><code>box[i].id = i;
  </code></pre>
  
  <p>However I am not totally satisfied with this because each time I reorder the array using <code>sort()</code> I have to run a loop to update the <code>id</code> properties with the new index values.</p>
  
  <p>My question is if there is a way to know inside an object its <strong>index</strong> in the array, without having to set the id property, hope you can help me.</p>
  
  <p>Thanks in advance.</p>