Say if you were unable to access a certain item inside an object through dot notation like objectName.objectItem
i.e. it has random name al the time. Is there a way to alternatively access it like second item?
how to get item2 from this object by only knowing that it is second item there?:
something: {
item1: "text",
item2: "text",
item3: "text:,
...
}
Order of keys are not fixed in javascript object.
You can try
Object keys have no order, but you can define your own binary relation that orders the keys.
Say, you only consider keys starting with
"item"
and a numeral suffix such asitem23
, then you can per your own definition translate this into the number23
and decide thatitem23
is the 23rd item in your array.Mind that this is completely arbitrary and only true in so far you want it to be so in your code.
That said, you can implement a function that filters all keys (considering only those starting with
"item"
), parse the numeral suffix into a number and then compare that to what index of an item you want.This code does exactly what I've supposed you want to do: