Access non-numeric Object properties by index?

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

    The only way I can think of doing this is by creating a method that gives you the property using Object.keys();.

    var obj = {
        dog: "woof",
        cat: "meow",
        key: function(n) {
            return this[Object.keys(this)[n]];
        }
    };
    obj.key(1); // "meow"
    

    Demo: http://jsfiddle.net/UmkVn/

    It would be possible to extend this to all objects using Object.prototype; but that isn't usually recommended.

    Instead, use a function helper:

    var object = {
      key: function(n) {
        return this[ Object.keys(this)[n] ];
      }
    };
    
    function key(obj, idx) {
      return object.key.call(obj, idx);
    }
    
    key({ a: 6 }, 0); // 6
    
    0 讨论(0)
  • 2020-11-27 03:42

    Get the array of keys, reverse it, then run your loop

      var keys = Object.keys( obj ).reverse();
      for(var i = 0; i < keys.length; i++){
        var key = keys[i];
        var value = obj[key];
        //do stuff backwards
      }
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-27 03:45
    var obj = {
        'key1':'value',
        '2':'value',
        'key 1':'value'
    }
    
    console.log(obj.key1)
    console.log(obj['key1'])
    console.log(obj['2'])
    console.log(obj['key 1'])
    
    // will not work
    console.log(obj.2)
    

    Edit:

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

    Actually the 'index' is the key. If you want to store the position of a key you need to create a custom object to handle this.

    0 讨论(0)
  • 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]])
    
    0 讨论(0)
  • 2020-11-27 03:52

    You can use the Object.values() method if you dont want to use the Object.keys().

    As opposed to the Object.keys() method that returns an array of a given object's own enumerable properties, so for instance:

    const object1 = {
     a: 'somestring',
     b: 42,
     c: false
    };
    
    console.log(Object.keys(object1));
    

    Would print out the following array:

    [ 'a', 'b', 'c' ]
    

    The Object.values() method returns an array of a given object's own enumerable property values.

    So if you have the same object but use values instead,

    const object1 = {
     a: 'somestring',
     b: 42,
     c: false
    };
    
    console.log(Object.values(object1));
    

    You would get the following array:

    [ 'somestring', 42, false ]
    

    So if you wanted to access the object1.b, but using an index instead you could use:

    Object.values(object1)[1] === 42
    

    You can read more about this method here.

    0 讨论(0)
提交回复
热议问题