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?

0

3 Answers 3

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that makes sense.
1

when acessing with dynamic attribute You should do as

theResponse[current] not theResponse.current

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.