Access non-numeric Object properties by index?

前端 未结 9 2411
时光取名叫无心
时光取名叫无心 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:48

    If you are not sure Object.keys() is going to return you the keys in the right order, you can try this logic instead

    var keys = []
    var obj = {
        'key1' : 'value1',
        'key2' : 'value2',
        'key3' : 'value3',
    }
    for (var key in obj){
        keys.push(key)
    }
    console.log(obj[keys[1]])
    console.log(obj[keys[2]])
    console.log(obj[keys[3]])
    

提交回复
热议问题