admin管理员组

文章数量:1289583

Jquery + Rails 4

<script>
 var jsonData = {
  "81404": "Object",
  "81408": "Object",
  "81416": "Object",
  "80387": "Object",
  "73952": "Object",
  "74697": "Object",
  "81411": "Object",
  "74700": "Object"
 };
console.log(jsonData);
</script>

Mozilla Output (Right, and expected)

Object { 81404="Object", 81408="Object", 81416="Object", 80387="Object", 73952="Object", 74697="Object", 81411="Object", 74700="Object"}

Chrome Output (Wrong, ???)

Object {73952: "Object", 74697: "Object", 74700: "Object", 80387: "Object", 81404: "Object", 81408: "Object", 81411: "Object", 81416: "Object"}

How to fix this automatically sorting issue in Chrome any suggestion help,,,

I am using this data for filtering that's order is important.

Jquery + Rails 4

<script>
 var jsonData = {
  "81404": "Object",
  "81408": "Object",
  "81416": "Object",
  "80387": "Object",
  "73952": "Object",
  "74697": "Object",
  "81411": "Object",
  "74700": "Object"
 };
console.log(jsonData);
</script>

Mozilla Output (Right, and expected)

Object { 81404="Object", 81408="Object", 81416="Object", 80387="Object", 73952="Object", 74697="Object", 81411="Object", 74700="Object"}

Chrome Output (Wrong, ???)

Object {73952: "Object", 74697: "Object", 74700: "Object", 80387: "Object", 81404: "Object", 81408: "Object", 81411: "Object", 81416: "Object"}

How to fix this automatically sorting issue in Chrome any suggestion help,,,

I am using this data for filtering that's order is important.

Share Improve this question edited Dec 7, 2024 at 3:20 mickmackusa 48.1k13 gold badges93 silver badges161 bronze badges asked Jun 16, 2014 at 10:36 user3676578user3676578 2231 gold badge6 silver badges17 bronze badges 5
  • 1 It would be useful to understand why you care? – musefan Commented Jun 16, 2014 at 10:41
  • 3 The fields of a JS object are not ordered, so both are "right". The order of fields of a JS object is not guaranteed. – Kaarel Nummert Commented Jun 16, 2014 at 10:41
  • 3 Your data is not an array. It has no intrinsic order. They are just properties on an object. Put them in an array property on the JSON object if order is important. – iCollect.it Ltd Commented Jun 16, 2014 at 10:41
  • stackoverflow./questions/5525795/… – Prinzhorn Commented Jun 16, 2014 at 10:46
  • This question is similar to: How do you stop Chrome and Opera sorting JSON objects by Index ASC?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – mickmackusa Commented Dec 7, 2024 at 3:20
Add a ment  | 

1 Answer 1

Reset to default 8

Your data is not an array. It has no intrinsic order. They are just properties on an object.

From this Reference

4.3.3 Object
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function.

Put them in an array property on the JSON object if order is important (or just use an array!).

e.g. something like:

var jsonData = {data: [
    {"81404": "Object"},
    {"81408": "Object"},
    {"81416": "Object"},
    {"80387": "Object"},
    {"73952": "Object"},
    {"74697": "Object"},
    {"81411": "Object"},
    {"74700": "Object"}]
 };
console.log(jsonData);

or for just the list

console.log(jsonData.data);

It would be helpful to explain what you are doing with the data, so that any example is more applicable.

本文标签: javascriptHow to stop json Data to automatically sorting in Google ChromeStack Overflow