0

enter image description here

I'm trying to use JSON.strigify() on my object (see screenshot).

However I get result which I'm not expecting, object indexes are in wrong order.

Full stringified json, you can see here: http://vpaste.net/LqNlq

As you can see first index is 9:0, but not 8:0 as expected.

What's the problem here ?

gdyrrahitis
  • 4,340
  • 3
  • 20
  • 36
user1876234
  • 807
  • 2
  • 13
  • 27
  • 3
    Unlike Arrays, Object properties are not ordered in any predictable way. Every browser does it its own way, and you cannot rely on this order. You cannot _sort_ an Object. – blex May 09 '16 at 19:38
  • 1
    You should look into using arrays for this, order of object properties is not guaranteed. http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – IrkenInvader May 09 '16 at 19:38
  • That's most probably Chrome. – Bekim Bacaj May 09 '16 at 19:48

1 Answers1

1

The keys of Objects in javascript aren't guaranteed to be in any order.

You should make it an Array of Objects instead to preserve order.

e.g.

{
  "1": [
    { "key": "8:0", ... },
    { "key": "8:30", ... },
    ...
  ],
  "2": ...
}

This should also be the same structure if you expect your top level keys ("1", "2", etc.) to be iterated over in order.

MattDiamant
  • 7,330
  • 4
  • 32
  • 45
  • All MSIE versions preserve the entry order. Fast wannabes don't! Because that's a naturally expected behavior. That's an empirical requirement, who cares if standard doesn't explicitly state it. If implementation leaves a crucial self understood requirement to browser vendor specific implementation, it doesn't mean that they're free to shuffle them simply because they are not explicitly required to preserve their order. – Bekim Bacaj May 09 '16 at 20:36
  • 2
    Everything you've said is a matter of opinion. In fact, since browsers can have different Object orderings, it's even more evidence that you shouldn't be iterating over Objects. Don't use Objects as Associative Arrays. There are more logical ways to structure your data. – MattDiamant May 09 '16 at 22:01
  • 1
    @BekimBacaj - saying that a particular way of doing it is a _"crucial self understood requirement"_ doesn't make it true. I've programmed ECMAScript for 10+ years without ever needing properties to have an order. Things with orders are held in Arrays. And _"leaving them free to shuffle them"_ is actually, in the most literal sense, what it means when the spec doesn't describe an order. – Jimbo Jonny May 09 '16 at 22:52
  • A blind man needs his eyes - nevertheless, he learnt to live without them. This doesn't mean he doesn't need to see. I've programmed JavaScript since 1996, and I still need a stable sort; I still need `isNS()` (which I had to write on my own), and we still need a static `typeOf`, complementing the dynamic `typeof` as does `===` complement `==` etc; We all know that we need to find things the way we've put them not the other way around - but you've learned to reroute your code and bypass that need. And that's wrong. – Bekim Bacaj May 09 '16 at 23:35