Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get json value dynamically

{
 "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
});
like image 677
anotherdev Avatar asked Jan 17 '23 10:01

anotherdev


1 Answers

You can use Bracket notation:

console.log( data['optionid_' + jsonid] );
like image 63
Paul Avatar answered Jan 29 '23 00:01

Paul