1

I have a string variable: id = '1';
I want to make a variable that holds the value of data.img + id + .dispWidth without making it a string.

For example, if id = '7'; then dw = data.img7.dispWidth;
How can I do this?

0

2 Answers 2

3

Use bracket notation to use dynamic property keys

dw = data['img' + id].dispWidth;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. Worked like a charm.
1

data['img' + id].dispWidth should do it. Although at that point you should probably consider whether you don't actually want an array.

1 Comment

Thanks for the help. Worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.