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
  • 2,180
  • 1
  • 28
  • 64

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
  • 102,349
  • 23
  • 137
  • 192
1

when acessing with dynamic attribute You should do as

theResponse[current] not theResponse.current

Arjun
  • 3,248
  • 18
  • 35
1

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

Itzik.B
  • 1,023
  • 2
  • 15
  • 35