admin管理员组文章数量:1391943
{
"id":["123"],
"optionid_123":"98"
}
I have the id
as a variable, but from that how can I get the optionid_*
? I tried a few things, but nothing seems to work. The each
is inside of the appropriate function and jsonid contains the correct value. Here is my attempt at accessing the value 98
which doesn't work:
$.each(data.id,function(){
var jsonid = this;
console.log( data.optionid_+jsonid ); // doesn't work
});
{
"id":["123"],
"optionid_123":"98"
}
I have the id
as a variable, but from that how can I get the optionid_*
? I tried a few things, but nothing seems to work. The each
is inside of the appropriate function and jsonid contains the correct value. Here is my attempt at accessing the value 98
which doesn't work:
$.each(data.id,function(){
var jsonid = this;
console.log( data.optionid_+jsonid ); // doesn't work
});
Share
Improve this question
edited Jun 12, 2012 at 1:05
Paul
142k28 gold badges284 silver badges271 bronze badges
asked Jun 11, 2012 at 1:08
anotherdevanotherdev
1037 bronze badges
2 Answers
Reset to default 9You can use Bracket notation:
console.log( data['optionid_' + jsonid] );
I think your loop with data.id
is not correct. That is
$.each(data.id, function() {..})
is incorrect.
For example if you data
looks like following:
var data = [{
"id":["123"],
"optionid_123":"98"
},
{
"id":["456"],
"optionid_456":"99"
}];
Then you need to loop over data
and get your required property.
$.each(data, function(index, val) {
var jsonid = val.id[0]; // as val.id is array so you need [0] to get the value
console.log(val['optionid_' + jsonid]); // bracket notation used
});
DEMO
本文标签: javascriptGet json value dynamicallyStack Overflow
版权声明:本文标题:javascript - Get json value dynamically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744714098a2621297.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论