Accessing a JavaScript's object property without knowing that property name

前端 未结 3 1309
北恋
北恋 2020-12-05 19:04

Situation

I have a JSON object which is returned. And Below is an example of one. The who in this particular example can change to what

3条回答
  •  [愿得一人]
    2020-12-05 19:53

    Object.keys(m)[0] should return the first enumerable property name in the object m.

    So if m = {"who": "Arthur"}; then m[Object.keys(m)[0]] will be "Arthur".

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys


    Alternatively: Object.values(m)[0]. See Object.values

提交回复
热议问题