0

In an array you can find the fifth value by:

array[4]

But can you do the same for objects? For example if I have:

test =
{
    tree: "epic",
    town: "cool",
    airport: "yay",
}

And then I want to use airport by its number, 3, how do I do that?

Ferus
  • 1,080
  • 3
  • 12
  • 17
  • Object properties have no ordering. The language spec doesn't require that the runtime produce property names in any particular order, in other words. So, no, not without keeping track of some ordering yourself. – Pointy Jun 28 '15 at 18:15
  • there is no order in key, value pairs, so no you cannot do what you mentioned. However you can design your array to be like test = [{tree: epic}, {town: cool} ...] – Rash Jun 28 '15 at 18:15
  • which number has airport? do you have some code to show? – Nina Scholz Jun 28 '15 at 18:15
  • @Rash: That makes it difficult because you still need to use the correct property name at the correct index. If there's to be a required order, a second array with just the property names as strings is better. `props = ["tree", "town", "airport"]` then `test[props[2]]` –  Jun 28 '15 at 18:18
  • Ok thanks guys, I know that it doesnt work now. – Ferus Jun 28 '15 at 18:29
  • You can actually find them by index. See [my answer](http://stackoverflow.com/a/31103463/3257901) on the original question. – Jeroen Vervaeke Jun 28 '15 at 18:48

0 Answers0