0

I've been having a lot of trouble with keeping the keys in a JSON object in their original state after they've been parsed by jQuery's $.each(). Even with string keys they are still being sorted into numerical order. Here is the JSON before parsed by jQuery:-

{
    "success": "true",
    "data": [
        {
            "row_13": {
                "id": "1_",
                "name": "_",
                "email": "_",
                "subject": "_",
                "body": "_",
                "ip": "_",
                "starred": "_"
            },
            "row_11": {
                "id": "_",
                "name": "_",
                "email": "_",
                "subject": "_",
                "body": "_",
                "ip": "_",
                "starred": "_"
            },
            "row_12": {
                "id": "_",
                "name": "_",
                "email": "_",
                "subject": "_",
                "body": "_",
                "ip": "_",
                "starred": "_"
            },
            "row_10": {
                "id": "_",
                "name": "_",
                "email": "_",
                "subject": "_",
                "body": "_",
                "ip": "_",
                "starred": "_"
            }
        }
    ]
}

However, once parse by jQuery, the order is as follows:-

"row_10", "row_11", "row_12", "row_13"

What on earth is causing this? It has been bugging me for at least a day now.

Polarize
  • 1,005
  • 2
  • 10
  • 27
  • 2
    There's no order in JS object properties (in ES5 at least). Use an array when you want to keep an ordered list. – Denys Séguret May 14 '15 at 18:08
  • Or, alternatively, have a property on the object that indicates its order. – Dylan Watt May 14 '15 at 18:11
  • If order matters, use an array of objects and the 12th row will be at index 11. – frenchie May 14 '15 at 18:11
  • So, when using an API, there is no way to keep the order at all? – Polarize May 14 '15 at 18:12
  • Maybe it's time to ditch jQuery. At least in Chrome, the order is as specified: http://jsfiddle.net/cLqL5Lg2/ – Felix Kling May 14 '15 at 18:16
  • @FelixKling I tried with jQuery and unsurprisingly it doesn't manage to create a different order. I don't think that my closing was right (now that we have a specified order). But this question is hardly answerable. I let you decide if it should be reopened or not. – Denys Séguret May 14 '15 at 19:01
  • @dystroy: I actually wrote an answer to the linked question, describing the changes in ES6. I don't have a strong opinion... – Felix Kling May 14 '15 at 19:03

0 Answers0