-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.

Community
  • 1
  • 1
Diego R
  • 77
  • 2
  • 9

1 Answers1

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

var output = data[myString][0].countryPhone1;
James M
  • 18,506
  • 3
  • 48
  • 56