Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interesting question... One plain JavaScript solution is to create an index for your objects in a separate array, based on the <code>'Surname'</code> property. Something like this<sup>1</sup>:</p> <pre><code>var peopleobj = { "0291" : { "Forename" : "Jeremy", "Surname" : "Dyson" }, "0398" : { "Forename" : "Billy", "Surname" : "Bunter" }, "6714" : { "Forename" : "Harry", "Surname" : "Peterson" }, "9080" : { "Forename" : "Barry", "Surname" : "Mainwaring" } }; var index = []; // build the index for (var x in peopleobj) { index.push({ 'key': x, 'Surname': peopleobj[x]['Surname'] }); } // sort the index index.sort(function (a, b) { var as = a['Surname'], bs = b['Surname']; return as == bs ? 0 : (as &gt; bs ? 1 : -1); }); </code></pre> <p>Now you would be able to iterate over your <code>index</code> array:</p> <pre><code>for (var i = 0; i &lt; index.length; i++) { console.log(peopleobj[index[i]['key']]['Surname']); } </code></pre> <p>Result (Tested in Firebug console):</p> <pre><code>Bunter Dyson Mainwaring Peterson </code></pre> <p>You may want to wrap this up into some sort of reusable Iterator object, even though it would be difficult to get as terse as Perl:</p> <pre><code>// Our reusable Iterator class: function MyIterator (o, key) { this.index = []; this.i = 0; this.o = o; for (var x in o) { this.index.push({ 'key': x, 'order': o[x][key] }); } this.index.sort(function (a, b) { var as = a['order'], bs = b['order']; return as == bs ? 0 : (as &gt; bs ? 1 : -1); }); this.len = this.index.length; } MyIterator.prototype.next = function () { return this.i &lt; this.len ? this.o[this.index[this.i++]['key']] : null; }; </code></pre> <p>Then use it as follows:</p> <pre><code>// Our JavaScript object: var peopleobj = { "0291" : { "Forename" : "Jeremy", "Surname" : "Dyson" }, "0398" : { "Forename" : "Billy", "Surname" : "Bunter" }, "6714" : { "Forename" : "Harry", "Surname" : "Peterson" }, "9080" : { "Forename" : "Barry", "Surname" : "Mainwaring" } }; // Build the Iterator object, using the 'Surname' field: var surnameIter = new MyIterator(peopleobj, 'Surname'); // Iterate: var i; while (i = surnameIter.next()) { console.log(i['Surname'] + ' ' + i['Forename']); } </code></pre> <p>Result: </p> <pre><code>Bunter Billy Dyson Jeremy Mainwaring Barry Peterson Harry </code></pre> <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="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> <pre><code>for (var x in peopleobj) { if (peopleobj.hasOwnProperty(x)) { index.push({ 'key': x, 'Surname': peopleobj[x]['Surname'] }); } } </code></pre>
    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. 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