Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:06:45+00:00 2026-06-14T04:06:45+00:00

This is a newbie JavaScript question, but something I’m not quite sure how to

  • 0

This is a newbie JavaScript question, but something I’m not quite sure how to google for help because I’m not sure how to describe the problem in an easy way.

I have a large, somewhat complex JSON that I need to manipulate so that I could reshape the JSON in a way that I get a list of only countries, cities, and sales.

The JSON itself isn’t the issue for me, it’s what I would like to do with it once I’ve received it. Basically, I’d like to create 3 separate objects/arrays from a large received JSON and have those 3 separate objects/arrays accessible for usage OUTSIDE of the $.ajax call. Yes, I think I could do all of this inside of the $.ajax success callback, but I’d rather have all the JSON processing done elsewhere. My pseudo JavaScript looks something like this:

var model = {
  countries: [],
  cities: [],
  sales: [],
  set: function(data) {
    //manipulate data here so that model.countries, model.cities, model.sales are populated
  }      
};


$.ajax({
  url: 'example.com/sample.json',
  success: function(data) {
    model.set(data);    //is this the right way to do this?
  }
});

$('#countries').html(model.countries);
$('#cities').html(model.cities);
$('#sales').html(model.sales);​

But because JavaScript executes asynchronously, the last 3 lines are always blank because the JSON hasn’t been received yet.

So I guess my question is, how do I bind the results of my received JSON to a variable outside of the $.ajax scope so that I could use it wherever on the page?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:06:46+00:00Added an answer on June 14, 2026 at 4:06 am

    The simple solution is this:

    $.ajax({
      url: 'example.com/sample.json',
      success: function(data) {
        model.set(data);
        $('#countries').html(model.countries);
        $('#cities').html(model.cities);
        $('#sales').html(model.sales);​
      }
    });
    

    If you want something more frameworky, then you could look at a something like Backbone.js.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of a javascript newbie so not sure if this question is easy or
Not sure if my question is subjective/objective but as a JavaScript newbie i'm encountering
Sorry for this newbie question, but I can't find on google what I need
This may be a newbie question. I've used javascript for years but have only
I'm absolutely sure this will be a trivial, newbie question, but hey - what's
Im a javascript newbie and would need some help with this simple thing... I
Sorry if this is a newbie question, searching google and SO turns up nothing,
I know that this is a newbie question but I am a newbie so
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
This is a newbie question: Can the following HTML/JavaScript code be further simplified by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.