sort keys with JSON.stringify()

Felipe Gasper felipe at felipegasper.com
Sat Sep 10 21:13:14 PDT 2011


But even that is implementation-specific, right?

Object key order can be completely random…at least, that’s always been 
my understanding.

-F

On 9/10/11 9:32 PM, Cryptic Swarm wrote:
> run it through something like this:
>
> function sortKeys(obj) {
>    var ret = {}
>    Object.keys(obj).sort().forEach(function(name) {
>      ret[name] = obj[name]
>    })
>    return ret
> }
>
> before passing it to JSON.stringify
>
>
> On Sat, Sep 10, 2011 at 9:14 PM, Felipe Gasper <felipe at felipegasper.com
> <mailto:felipe at felipegasper.com>> wrote:
>
>     Hello,
>
>             Is it possible to get sorted keys:
>
>     {a:1,c:3}
>
>     …from running JSON.stringify when declaring an object thus:
>
>     {c:3,a:1}
>
>     ??
>
>     -FG
>     _________________________________________________
>     es-discuss mailing list
>     es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>     https://mail.mozilla.org/__listinfo/es-discuss
>     <https://mail.mozilla.org/listinfo/es-discuss>
>
>



More information about the es-discuss mailing list