Get element of JS object with an index

后端 未结 7 1163
南方客
南方客 2021-01-30 20:43

Ok so let\'s say that I have my object

myobj = {\"A\":[\"Abe\"], \"B\":[\"Bob\"]}

and I want to get the first element out of it. As in I want i

7条回答
  •  不思量自难忘°
    2021-01-30 21:10

    JS objects have no defined order, they are (by definition) an unsorted set of key-value pairs.

    If by "first" you mean "first in lexicographical order", you can however use:

    var sortedKeys = Object.keys(myobj).sort();
    

    and then use:

    var first = myobj[sortedKeys[0]];
    

提交回复
热议问题