What is the best way to input variables into json values? [JavaScript]

let countryCode;
let countryName;
$(window).on("load", function () {
  $.ajax({
    url: "URL_THAT_RETURNS_JSON"
  }).done(function (json) {
    countryCode = json.country_code;

    $(function () {
      $.ajax({
        url: "URL_THAT_RETURNS_JSON"
      }).done(function (countryData) {
        countryName = countryData[countryCode];
      })
    })
  })
})

I am storing two variables: countryCode and countryName. The initial ajax call retrieves a value like this example:

{ .... "country_code":"US" .... }
, so I assign the country code to the countryCode variable. The second ajax call fetches data in this format:
{ "AF":"Afghanistan", "AL":"Albania", ... "ZW":"Zimbabwe" }
. I am attempting to match the country code from the first variable with the corresponding country name from the second ajax call, which is stored inside the countryName variable.
For instance => Suppose the first ajax call returned "US", assigning "US" to the countryCode variable. Now, how can I trigger the second ajax call so that the countryName variable will contain "United States"?
Given that in the json file we have "US":"United States".

Answer №1

Swap out country.code with country[code] for a different approach.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve the value from the selected radio button

Below is the provided HTML: <form> <div class="form-group"> <div class="checkbox-detailed male_input"> <input type="radio" name="gender" id="male" value="male"> <label for="male"> ...

Adding information to a database by utilizing Jquery, Ajax, and PHP

Trying to use ajax to submit data to a database has been a challenge for me. Even with a simple code test, I can't seem to make it work. Here is the HTML/ajax code snippet: <?php include("osb.php");?> <script type = "text ...

The initial value in useEffect is not being updated by useState

I am currently implementing a like feature for my web application. The issue lies in the fact that my setLike function is not updating the state even after using setLike(!like). I verified this by adding console.log() statements before and after the setLik ...

What are the steps to send AJAX data before closing the page?

Trying for over 7 hours to send a value to the database when the user closes the page, like online and offline. Still struggling to find a working solution. <script tysssspe="text/javascript"> //ST window.onbeforeunload = function(){ var user_st = ...

Customized slider in jQuery UI allowing users to select height using a scaled ruler

Currently, I am tackling a challenging math problem related to adjusting the height selector input. Essentially, I have implemented a jQuery UI slider for choosing a height. It operates in inches and ranges from 0 to 120 (equivalent to 10 feet in height). ...

ACTUAL preventing a component from losing its focus

I have been exploring ways to effectively stop a DOM element from losing focus. While researching, I came across different solutions on StackOverflow such as: StackOverflow solution 1 StackOverflow solution 2 StackOverflow solution 3 However, these sol ...

Having trouble with React MaterialUI <ListItemSecondaryAction> getting stuck while dragging within a react-beautiful-dnd Draggable?

I am currently utilizing react-beautiful-dnd to create draggable list items with the help of Material UI ListItems. Each of my ListItems consists of a ListItemText and a ListItemSecondaryAction which acts as a target for triggering a context menu (enclosi ...

Revolutionize iPad user experience with seamless HTML5 video integration

What is the most effective method for dynamically embedding HTML5 video to ensure compatibility with the iPad? (using pure Javascript) I'm having difficulty getting this approach to work: <div id="placeholder"><script type="text/javascript" ...

Facing a Null Pointer Exception when Deserializing Gson?

When attempting to parse JSON data in order to extract certain values, I encountered a particular issue. { "username":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a1babdb0babba692b5bfb3bbbefcb1bdbf">[email protect ...

The functionality of jQuery Isotope is not activated until the filter buttons are clicked

I am facing an issue where Isotope does not start when my webpage loads. I attempted to implement Isotope (from isotope.metafizzy.co) along with filtering buttons. Below is the HTML and JS code I am using: <div class="scroll-pane"> <div ...

How can I personalize the color of a Material UI button?

Having trouble changing button colors in Material UI (v1). Is there a way to adjust the theme to mimic Bootstrap, allowing me to simply use "btn-danger" for red, "btn-success" for green...? I attempted using a custom className, but it's not function ...

Exploring the Integration of Callbacks and Promises in Express.js

I'm currently developing an Express.js application where I am utilizing x-ray as a scraping tool to extract information. For each individual website I scrape, I intend to establish a unique model due to the distinct data and procedures involved in th ...

Is there a way to switch between showing and hiding all images rather than just hiding them one by one?

Is there a way I can modify my code to create a button that toggles between hiding and showing all images (under the user_upload class), instead of just hiding them? function hidei(id) { $('.user_upload').toggle(); Any suggestions would be grea ...

JavaScript and jQuery are lightning fast, especially when the page is reloaded

I am currently working on a responsive website that uses liquid layouts. I have encountered challenges when incorporating images in the design, especially when dealing with different browsers like IE, Firefox, and Chrome. Recently, I faced another issue w ...

What is the reason behind using AJAX to attempt sending a new URL request on

Having a strange issue with my product list. When trying to edit a product by clicking on it, I am redirected to the product form where an AJAX request is supposed to be sent to getFiltersGroup. However, on error, the AJAX request is somehow being sent to ...

Exploring Real-Time Search with ASP.NET MVC and AJAX

Currently, I am trying to integrate a live search feature into my MVC application that functions similarly to the search functionality on . This involves entering a query or question and relevant results will be displayed. I have successfully implemented ...

A guide to efficiently extracting the accurate ID from JSON using JSONPath syntax in Gatling

My current testing setup involves Gatling for API testing. In one of my scenarios, I need to extract an ID from a JSON response and save it in a variable. However, Gatling doesn't directly support this operation. The specific ID I am looking for corre ...

Combining two numbers retrieved from Firebase using React

Hello, I am new to React and finding it challenging to perform mathematical calculations with React. I have been attempting to add two values retrieved from a Firebase database, but they keep displaying as strings without adding the actual values together. ...

What is the best way to create floating text that appears when clicked, similar to the mechanics

https://i.stack.imgur.com/nRKG1.jpg Upon clicking the "big cookie" in my game, a popup appears displaying the number of cookies earned (like +276.341 septillion in this image). The popup slowly moves upward and then fades out. I successfully incorporated ...

In Laravel, I am facing an issue where I am unable to trigger any event on the data that I have appended using

Currently using Laravel 5.6 Implemented the following event to append div.categoryAjaxId: The code works as expected and successfully appends my content. //--------- get product ------------------------- jQuery('.orderProduct').click(function( ...