Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you are confusing JavaScript <a href="http://en.wikipedia.org/wiki/Array_data_structure" rel="nofollow">array objects</a> with the fact that <em>all</em> objects are <a href="http://en.wikipedia.org/wiki/Associative_array" rel="nofollow">associative arrays</a>. Note that normal objects (e.g. the "Fruit" object you allude to) do not have an intrinsic ordering of properties (keys) whereas Array objects do (due to the natural ordering of integral indices). Essentially, an Array is just an object with a special "length" property that stores the last integer index (starting from zero) plus one.</p> <p>Any object's properties will be iterated in <em>arbitrary</em> (e.g. random) order:</p> <pre class="lang-js prettyprint-override"><code>var obj = {a:'Aye', b:'Bee', c:'See', d:'Dee'}; for (var prop in obj) { alert(prop + '=' + obj[prop]); // No order is guaranteed. } </code></pre> <p>Strictly speaking, even arrays are not guaranteed by the specification to iterate in natural order using a <code>for-in</code> loop; however, most JavaScript interpreters do so anyway. (Notably, IE8 iterates in the order that indices were assigned, not natural order.) This is why you should always iterate arrays using indices in a plain "for" loop.</p> <pre class="lang-js prettyprint-override"><code>var arr = ['a', 'b', 'c', 'd', 'e', 'f']; for (var i=0; i&lt;arr.length; i++) { // i = [0 .. length-1] alert(arr + '=' + arr[i]); // Explicitly order as 'a', 'b', 'c'... } </code></pre> <p>These differences mean that regardless of how your "Fruit" object is defined there is no reliable way to ensure a strict ordering of keys (e.g. "Banana", "Apple", "Color", "Size", etc.) unless you retain your own ordering separately.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload