javascriptjqueryjsongoogle-chromeruby-on-rails-4

How to stop json Data to automatically sorting in Google Chrome?


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.


Solution

  • 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.