1

I have a JavaScript object written in javascript code as below:

var rtnStr = {"000":"area000","020":"area020","030":"area030",
              "040":"area040","047":"area047","049":"area049",
              "050":"area050","060":"area060","070":"area070",
              "100":"area100", "900":"area900"};

for (var key in rtnStr) {
  document.write('rtnStr[' + key + ']= ' + rtnStr[key] + '<br />');
}

then I run the code in browser, I got the result:

rtnStr[100]= area100
rtnStr[900]= area900
rtnStr[000]= area000
rtnStr[020]= area020
rtnStr[030]= area030
rtnStr[040]= area040
rtnStr[047]= area047
rtnStr[049]= area049
rtnStr[050]= area050
rtnStr[060]= area060
rtnStr[070]= area070

the object fields(keys) sequence is not the same as I have declare, How can I got the result as below?

rtnStr[000]= area000
rtnStr[020]= area020
rtnStr[030]= area030
rtnStr[040]= area040
rtnStr[047]= area047
rtnStr[049]= area049
rtnStr[050]= area050
rtnStr[060]= area060
rtnStr[070]= area070
rtnStr[100]= area100
rtnStr[900]= area900
4
  • 1
    That's JavaScript, not JSON. Commented Dec 17, 2013 at 13:16
  • its because obects have no defined order. Commented Dec 17, 2013 at 13:17
  • possible duplicate of Elements order in a "for (… in …)" loop Commented Dec 17, 2013 at 13:17
  • Object properties are unordered. Commented Dec 17, 2013 at 13:18

3 Answers 3

1

JavaScript objects are unordered.

If you want to output their properties in a sorted order then:

  1. Create an array
  2. Loop over the object, pushing the property names into the array
  3. Sort the array
  4. Loop over the array (and use the values to access the original object).
Sign up to request clarification or add additional context in comments.

Comments

0
 var rtnStr = {"000":"area000","020":"area020","030":"area030",
              "040":"area040","047":"area047","049":"area049",
              "050":"area050","060":"area060","070":"area070",
              "100":"area100", "900":"area900"};

  array_ = [];

  for (var key in rtnStr) {
    array_.push(key)
  }

  array_.sort();


  for (var key in array_) {
    document.write('rtnStr[' + key + ']= ' + rtnStr[key] + '<br />');
  }

Comments

0

As mentioned by @Quentin, the order of the properties in JavaScript objects is undefined. The spec doesn't even mention if the properties will be returned in the same order by different for..ins.

What you can do is get the keys in an array and sort them:

var rtnStr = {"000":"area000","020":"area020","030":"area030",
              "040":"area040","047":"area047","049":"area049",
              "050":"area050","060":"area060","070":"area070",
              "100":"area100", "900":"area900"};

var keys = Object.keys(rtnStr).sort().forEach(function(key){
  document.write('rtnStr[' + key + ']= ' + rtnStr[key] + '<br />');
});

MDN has a page for Object.keys where you can find the browsers it works on and a polyfill for those it doesn't.

One thing to note is that your initial version of the code iterates over inherited properties as well. That is generally considered to be unsafe and Object.keys doesn't exhibit that behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.