Selecting an object property by a name stored in a variable

Let’s say I have the following object:

const person = {
  name: 'john',
  age: 19,
  gender: 'm'

And I have a variable field whose value can be "name", "age" or "gender". I want to access person.field (obviously that syntax doesn’t work), such that if field equals "gender" then the result should be "m".

How to achieve that?

135 thoughts on “Selecting an object property by a name stored in a variable”

  1. Pingback: stromectol is for
  2. Please let me know if you’re looking for a author for
    your blog. You have some really great articles and I believe
    I would be a good asset. If you ever want to take some
    of the load off, I’d really like to write some articles for your blog
    in exchange for a link back to mine. Please blast me an email if interested.
    Many thanks!

  3. Pingback: ivermectin dosing
  4. Pingback: ivermectin 100 mg
  5. Pingback: apo tadalafil 20mg
  6. Pingback: brand viagra 50 mg
  7. Pingback: sublingual viagra

Leave a Comment