admin管理员组文章数量:1418056
In json can we get attribute value by passing variably value . Means
It works for me when "name" attribute exists in my "returnData" json object
// It works
var getColValue= returnedData[0].name
but it give undefined error
// It Not works
var refVar ="name";
var getColValue= returnedData[0].refVar;
In json can we get attribute value by passing variably value . Means
It works for me when "name" attribute exists in my "returnData" json object
// It works
var getColValue= returnedData[0].name
but it give undefined error
// It Not works
var refVar ="name";
var getColValue= returnedData[0].refVar;
Share
Improve this question
edited Dec 15, 2012 at 7:30
user1905838
asked Dec 15, 2012 at 7:19
user1905838user1905838
111 silver badge3 bronze badges
1
- 2 yeah, if returnedData doesn't have refVar property, you try to access it then you will receive "undefined" value. To make it work, you can try returnedData[refVar] jsfiddle/wsyCP/2 – secretlm Commented Dec 15, 2012 at 7:33
2 Answers
Reset to default 2var getColValue= returnedData[refVar];
should work. Please give it a try.
Use square bracket notation:
returnedData[refVar];
In other words, these two are basically equivalent:
returnedData["name"] === returnedData.name
Note that, using square-bracket notation allows you to set/get property names that wouldn't be valid with the dot notation. Eg, returnedData.some-prop
is not a valid Javascript object, but returnedData["some-prop"]
is.
本文标签: Get JSON attribute value by paasing attribute name via quotvarquot in JavascriptStack Overflow
版权声明:本文标题:Get JSON attribute value by paasing attribute name via "var" in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258932a2650264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论