1

For example, if I have an object like:

{"angeredoutsidecontrol":"1","difficultiespileup":"2"}

And then later in a for loop I can access the key of angeredoutsidecontrol , how can I get the value returned as 0, which would represent which place in the object this key is?

Anthony
  • 13,434
  • 14
  • 60
  • 80

3 Answers3

2

There is no guaranteed order for keys of an object.

Definition of object from an old - but still effective in this case - documentation:

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. A function stored in a property of an object is called a method.

If order really matters to you, use array instead. For example:

[{ "angeredoutsidecontrol": "1" }
 { "difficultiespileup": "2" }];
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
0

The order of JSON objects is not maintained, so you can't do this.

[Is the order of elements in a JSON list maintained?

Paul Cuddihy
  • 477
  • 6
  • 12
0
var myMoods = ["angeredoutsidecontrol","difficultiespileup"];

and

myMoods.indexOf( 'angeredoutsidecontrol' ) 

gives you position in your list

Jarek Kulikowski
  • 1,399
  • 8
  • 9