user2873816
user2873816

Reputation: 179

How to convert string to symbol in javascript

I have json data in the following format.

 var jsonData={
"India":["Australia","New Zealand","Pakistan","England"],
"Pakistan":["New Zealand","India","England"],
"Bangladesh":["New Zealand","India","England","SouthAfrica","Australia"],
"Srilanka":["New Zealand","India","England","SouthAfrica","Australia"],
"Australia":["New Zealand","Pakistan","India"],
"New Zealand":["Pakistan","India","England","SouthAfrica","Australia"],
"SouthAfrica":["Srilanka","India","Pakistan"],
"England":["Srilanka","New Zealand","Pakistan","Bangladesh"]
 };

I will get country name from dropdown list.it is selected by user.

  var countryName=document.getElementById("pop1").value;

Now I want to fetch specific country data from json data based on countryName value.

for example.If countryName value is equals to India then it should returns jsonData.India data.

How to do this.

Thanks

Upvotes: 4

Views: 2312

Answers (2)

Mariusz Jamro
Mariusz Jamro

Reputation: 31643

Like that:

var countryName = 'India';
var countries = jsonData[countryName];

Upvotes: 0

antyrat
antyrat

Reputation: 27765

You need to pass your variable like this:

jsonData[ countryName ]

So basically you can access object properties in JS by 2 ways:

sobeObj.value

or

someObj[ 'value' ]

Inside brackets can be string or variable that represent the string.

Upvotes: 4

Related Questions