javascript - accessing objects property dynamically (appending a string) -
i have loop in underscore
_.each(questions,function(data){ a="reason"+data.choosen; %> <%= data.a; %> <% count++; }); %>
the data.choosen gets either 1 or 2 or 3 or 4. based on want display data.reason1 or data.reason2 or data.reason3 or data.reason4 property of data object.
i tried above approach doesn't work.
in javascript
x.y
is equivalent to
x["y"]
so can change code to
data[a]
Comments
Post a Comment