0
var code;
var Name;
$(window).on("load", function () {
  $.ajax({
    url: "URL_THAT_RETURNS_JSON"
  }).done(function (json) {
    code = json.country_code;

    $(function () {
      $.ajax({
        url: "URL_THAT_RETURNS_JSON"
      }).done(function (country) {
        Name = country.code ?
      })
    })
  })
})

I have a variable code and Name. The top function calls json and returns this value for example: { .... "country_code":"US" .... } so I put the country code value in the variable code. And the second ajax function will call a json file that looks like this: { "AF":"Afghanistan", "AL":"Albania", ... "ZW":"Zimbabwe" } I am trying to call the second variable with the country code I have, which is in side the code variable. Can I call the country code function and assign that into the Name variable?
For example => The first ajax function returned "US". So the code variable now has "US" inside. Now can I in anyway call the second ajax function so that the Name variable will have the value "United States"?
But in the json file, we have "US":"United States".

V. Sambor
  • 12,361
  • 6
  • 46
  • 65
EduCheck
  • 11
  • 1
  • 2
  • Depending on how the second API is called, you might just need to use square bracket notation: `Name = country[code]` – TommyBs Feb 05 '20 at 13:42
  • I am using a github respository, and it goes like .../countrycode.json – EduCheck Feb 05 '20 at 13:43
  • 1
    If your var `code` contains the code of the country (ex: AF, AL, US, etc...) and if your second ajax call returns an object like `{ "AF": "...", "AL": "...", ... }`, you should use `country[code]` to get the value for the key `code` in the object `country`. You should read about how work with javascript objects. https://www.w3schools.com/js/js_objects.asp – robinvrd Feb 05 '20 at 13:43
  • Yes! The country[code] worked, thanks everybody! – EduCheck Feb 05 '20 at 13:44

1 Answers1

1

Instead of using country.code, change it to country[code].

acagastya
  • 323
  • 2
  • 14