Qingping Qingping - 1 year ago 89
TypeScript Question

How to use an object's value as the parameter for another object in Javascript?

If I have:

var key = "Section";
course.key = "101";

I get told
course.key is unidentified
. I'm trying to set
course.Section = "101"
. Is there anyway to pass key's value in the second line?

Answer Source

The property key of course is undefined. Use bracket notation.

course[key] = "101"

is the same as

course["Section"] = "101"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download