Chris Marshall
Chris Marshall

Reputation: 5332

JavaScript: Specifying the Order of Properties In An Object

OK. I've been looking at the supplied answers, and they all seem to be very specific. I need to know this in a general sense.

ISSUE

I have a couple of JS objects. I use JSON.stringify to compare them (see if an object is "dirty").

However, stringify renders the object exactly as it is set in memory, so it takes whatever order the properties of that object happen to be in.

If I delete a property, then restore it, that could change the order.

I'll show a simple HTML example in a second. What I want, is for the RESTORE section to look exactly like the BEFORE section.

I'm wondering if there is a basic, browser-generic way to do that.

EXAMPLE

<html>
    <head><title>Test JS</title></head>
    <body>
        <dl>
            <dt><strong><big>BEFORE:</big></strong></dt>
                <dd><em><strong>Object A:</strong> <span id="objA1"></span></em></dd>
                <dd><em><strong>Object B:</strong> <span id="objB1"></span></em></dd>
            <dt><strong><big>AFTER:</big></strong></dt>
                <dd><em><strong>Object A:</strong> <span id="objA2"></span></em></dd>
                <dd><em><strong>Object B:</strong> <span id="objB2"></span></em></dd>
            <dt><strong><big>RESTORE:</big></strong></dt>
                <dd><em><strong>Object A:</strong> <span id="objA3"></span></em></dd>
                <dd><em><strong>Object B:</strong> <span id="objB3"></span></em></dd>
        </dl>
        <script type="text/javascript">
            var objA, objB;

            function setUpObjects ( ) {
                objA = new Object;
                objB = new Object;

                objA.someProperty = 'HIHOWAYA';
                objB.someProperty = 'HIHOWAYA';
                objA.someOtherProperty = 'JUSTFINETHX';
                objB.someOtherProperty = 'JUSTFINETHX';
            };

            function deleteAProperty ( ) {
                delete objA.someProperty;
            };

            function restoreAProperty ( ) {
                objA.someProperty = 'HIHOWAYA';
            };

            function displayObjects ( inIndex ) {
                document.getElementById ( 'objA' + inIndex.toString() ).innerHTML = JSON.stringify ( objA );
                document.getElementById ( 'objB' + inIndex.toString() ).innerHTML = JSON.stringify ( objB );
            };

            setUpObjects();
            displayObjects ( 1 );
            deleteAProperty();
            displayObjects ( 2 );
            restoreAProperty();
            displayObjects ( 3 );

        </script>
    </body>
</html>

Upvotes: 0

Views: 45

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074138

I'm wondering if there is a basic, browser-generic way to do that.

No. You'll have to create your own JSON serializer if you want to ensure that the properties in the JSON text are output in a defined order, as neither JSON nor JavaScript apply order to the properties. This is only useful in that you can then compare texts in a meaningful way; once you parse that text, order is once again not guaranteed.

Upvotes: 1

Valentin Waeselynck
Valentin Waeselynck

Reputation: 6051

Javascript objects - like most basic hash table data structures - offer no guarantee as to the order of their keys: you should not rely on whatever order you observe empirically. If you want to iterate over the properties in a robust, predictable order, you'll have to complement your objects with some other data structure, e.g an array of the keys.

Upvotes: 2

Related Questions