0

I'm trying to access a value in the array by passing in the key, but it returns undefined.

 const names = {
      aegon: 'targaryen',
      arya: 'stark',
      cersei: 'lannister'
    }

    let keys = Object.keys(names);

    console.log(keys['aegon'])
Josh Adams
  • 2,113
  • 2
  • 13
  • 25
JorahFriendzone
  • 439
  • 1
  • 4
  • 21

1 Answers1

0

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually. See Here

I think you are trying to access the value, you could do so with names.aegon

const names = {
      aegon: 'targaryen',
      arya: 'stark',
      cersei: 'lannister'
    }

    let keys = Object.keys(names);

    console.log(keys[0]);
    console.log(names.aegon);
Josh Adams
  • 2,113
  • 2
  • 13
  • 25