0

I have a local state object that looks will look like this:

state:{
   pickup:{
      blah: stuff
    }
}

OR

state:{
   dropoff:{
      blah: stuff
    }
}

What I am trying to do is grab either the pickup or dropoff object and set it to some other variable.

This is being done in a function that is passing in whichever type we are attempting to pull off.

tl;dr How can I do something like const location = this.state.[locationType] where locationType is passed into my function.

Nobody
  • 159
  • 2
  • 3
  • 11
  • 2
    If `locationType` is a string, just drop the `.` and do `const location = this.state[locationType]` – Nicholas Tower Nov 15 '17 at 19:54
  • 1
    you want a dynamic key in state? whyyyyyyy – Ted Nov 15 '17 at 19:55
  • 1
    @Ted sorry, maybe I was unclear. Both `pickup` and `dropoff` exist in state, I am just attempting to access a particular one that is designated by a particular component. – Nobody Nov 15 '17 at 20:00

1 Answers1

0

Following @Nick Tower's comment,

const location = this.state[locationType]

produced the desired result.

Nobody
  • 159
  • 2
  • 3
  • 11