1

I am getting several arrays returned from a GET reponse, which I need to access. Since their names can change based on the request, I have a variable specifing which array to use.

However I always get undefined. See this:

            console.log(current); // trips_out_201702
            console.log(theResponse.current); // undefined
            console.log(theResponse.trips_out_201702); // this works

How can I make theResponse.current work such that it returns what current actually stands for? Why do I get undefined there?

ffritz
  • 1,912
  • 1
  • 24
  • 50

3 Answers3

1

When the property key in an object is a variable you can use the square bracket notation to access that property.

  console.log(theResponse[current]);
Vincent Ramdhanie
  • 100,586
  • 22
  • 140
  • 187
1

when acessing with dynamic attribute You should do as

theResponse[current] not theResponse.current

Arjun
  • 2,777
  • 14
  • 32
1

You are trying to get array value using object's way. You should try this one instant variable['keyName'] Good Luck!

Itzik.B
  • 927
  • 2
  • 12
  • 32