-1

How do I replace the "Argentina" key with a javascript variable string?

jQuery(document).ready(function() {

    $.getJSON('countries.json', function(data) {

        var output= data.Argentina[0].countryPhone1;

        alert(output);

     });
});

Here's what my .json data file looks like:

{
    "Argentina": [{
        "countryName":"Argentina",
        "countryPhone1":"1234"
    }],

    "Brazil": [{
        "countryName":"Brazil",
        "countryPhone1":"5678"
    }]
}

I tried applying the suggested solutions here, here and here, but I couldn't figure out a way to adapt them to my example.

0

1 Answer 1

0
var myString = 'Argentina';  /* or whatever you like */

var output = data[myString][0].countryPhone1;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.