Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simpler, modern and currently browser supported approach is simply this:</p> <pre><code>JSON.stringify(sortMyObj, Object.keys(sortMyObj).sort()); </code></pre> <p>However, this method does remove any nested objects that aren't referenced. You will want to flatten the sorting object as well if you want something like this output:</p> <pre><code>{"a":{"h":4,"z":3},"b":2,"c":1} </code></pre> <p>You can do that with this:</p> <pre><code>var flattenObject = function(ob) { var toReturn = {}; for (var i in ob) { if (!ob.hasOwnProperty(i)) continue; if ((typeof ob[i]) == 'object') { var flatObject = flattenObject(ob[i]); for (var x in flatObject) { if (!flatObject.hasOwnProperty(x)) continue; toReturn[i + '.' + x] = flatObject[x]; } } else { toReturn[i] = ob[i]; } } return toReturn; }; JSON.stringify(sortMyObj, Object.keys(flattenObject(sortMyObj)).sort()); </code></pre> <p>To do it programmatically with something you can tweak yourself, you need to push the object property names into an array, then sort the array alphabetically and iterate through that array (which will be in the right order) and select each value from the object in that order. "hasOwnProperty" is checked also so you definitely have only the object's own properties. Here's an example:</p> <pre><code>var obj = {"a":1,"b":2,"c":3}; function iterateObjectAlphabetically(obj, callback) { var arr = [], i; for (i in obj) { if (obj.hasOwnProperty(i)) { arr.push(i); } } arr.sort(); for (i = 0; i &lt; arr.length; i++) { var key = obj[arr[i]]; //console.log( obj[arr[i]] ); //here is the sorted value //do what you want with the object property if (callback) { // callback returns arguments for value, key and original object callback(obj[arr[i]], arr[i], obj); } } } iterateObjectAlphabetically(obj, function(val, key, obj) { //do something here }); </code></pre> <p>Again, this should guarantee that you iterate through in alphabetical order. </p> <p>Finally, taking it further for the simplest way, this library will recursively allow you to sort any JSON you pass into it: <a href="https://www.npmjs.com/package/json-stable-stringify" rel="noreferrer">https://www.npmjs.com/package/json-stable-stringify</a></p> <pre><code>var stringify = require('json-stable-stringify'); var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; console.log(stringify(obj)); </code></pre> <p>Output</p> <pre><code>{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8} </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. 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