Access non-numeric Object properties by index?

前端 未结 9 2412
时光取名叫无心
时光取名叫无心 2020-11-27 03:41

If I have an array like this:

var arr = [\'one\',\'two\',\'three\'];

I can access different parts by doing this:

console.lo         


        
9条回答
  •  醉话见心
    2020-11-27 03:44

    "I'm specifically looking to target the index, just like the first example - if it's possible."

    No, it isn't possible.

    The closest you can get is to get an Array of the object's keys, and use that:

    var keys = Object.keys( obj );
    

    ...but there's no guarantee that the keys will be returned in the order you defined. So it could end up looking like:

    keys[ 0 ];  // 'evenmore'
    keys[ 1 ];  // 'something'
    

提交回复
热议问题