Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:,
  ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
444 views
Welcome To Ask or Share your Answers For Others

1 Answer

Order of keys are not fixed in javascript object.

You can try

var secondKey = Object.keys(something)[1]; //fetched the key at second index
alert(something[secondKey ]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...