html - JavaScript: Specifying the Order of Properties In An Object -
ok. i've been looking @ supplied answers, , seem specific. need know in general sense.
issue
i have couple of js objects. use json.stringify compare them (see if object "dirty").
however, stringify renders object set in memory, takes whatever order properties of object happen in.
if delete property, restore it, change order.
i'll show simple html example in second. want, restore section before section.
i'm wondering if there basic, browser-generic way 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>
i'm wondering if there basic, browser-generic way that.
no. you'll have create own json serializer if want ensure properties in json text output in defined order, neither json nor javascript apply order properties. useful in can compare texts in meaningful way; once parse text, order once again not guaranteed.
Comments
Post a Comment