Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Objects in JavaScript are collections of <em>unordered</em> properties. Objects are hashtables. </p> <p>If you want your properties to be in alphabetical order, one possible solution would be to create an index for your properties in a separate array. Just a few hours ago, I answered a question on Stack Overflow which you may want to check out:</p> <ul> <li><a href="https://stackoverflow.com/questions/4041762/iterating-over-a-javascript-object-in-sort-order-based-on-particular-key-value-of/4041878#4041878">Iterating over a JavaScript object in sort order based on particular key value of a child object</a></li> </ul> <p>Here's a quick adaptation for your object<sup>1</sup>:</p> <pre><code>var obj = { "set1": [1, 2, 3], "set2": [4, 5, 6, 7, 8], "set3": [9, 10, 11, 12] }; var index = []; // build the index for (var x in obj) { index.push(x); } // sort the index index.sort(function (a, b) { return a == b ? 0 : (a &gt; b ? 1 : -1); }); </code></pre> <p>Then you would be able to do the following:</p> <pre><code>console.log(obj[index[1]]); </code></pre> <p>The <a href="https://stackoverflow.com/questions/4041762/iterating-over-a-javascript-object-in-sort-order-based-on-particular-key-value-of/4041878#4041878">answer I cited</a> earlier proposes a reusable solution to iterate over such an object. That is unless you can change your JSON to as <a href="https://stackoverflow.com/questions/4044845/retrieving-a-property-of-a-json-object-by-index/4044892#4044892">@Jacob Relkin suggested in the other answer</a>, which could be easier.</p> <hr> <p><sup>1</sup> You may want to use the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" rel="nofollow noreferrer"><code>hasOwnProperty()</code></a> method to ensure that the properties belong to your object and are not inherited from <code>Object.prototype</code>.</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