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 1001437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:40:20+00:00 2026-05-16T07:40:20+00:00

How do you store the data received from jQuery getJSON to an array for

  • 0

How do you store the data received from jQuery getJSON to an array for later usage?

Here’s a sample snippet – somehow the loop’s data is not being stored to the global haikus.


$(document).ready(function(){
    var haikus=[];
    alert("begin loop");
    $.getJSON('http://haikuennui.com/random.php',function(data){
         var i=0;
         for(i=0;i<data.length;i++){
            haikus[i]=[data[i].id,String(data[i].username),String(data[i].haiku)];
        }
            alert(haikus[0][1]);
    });
})
​

http://jsfiddle.net/DMU2v/

  • 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-05-16T07:40:21+00:00Added an answer on May 16, 2026 at 7:40 am

    If I’m understanding your question correctly, you want to cache a number of AJAX-retrieved items?

    So if all the items look like this, say:

    { id: 1, value: 'Test' }
    

    … and you don’t want to AJAX-fetch the value for ID=1 if you’ve already done it once…?

    In that case, declare a cache variable somewhere in global scope:

    var ajaxCache = {};
    

    On success of your retrieve function, add to it:

    ajaxCache['item' + item.id] = item;
    

    When you’ve done that, you can modify your retrieve function as such:

    if(('item' + id) in ajaxCache) {
        return ajaxCache['item' + id];
    }
    
    // continue to fetch 'id' as it didn't exist
    

    It should be noted that this is not actually an array. The reason I didn’t use an array, is that assigning an item with ID 2000 would give the array a length of 2001, instead of just adding a property to it. So regardless of how you approach it, iterating from 0 to array.length will never be a good way of getting all items (which is the only scenario where the difference between an array and an object will matter, in this particular context).

    Instead, to iterate this object, you need to write

    for(var key in ajaxCache) {
       var item = ajaxCache[key];
    
       // whatever you want to do with 'item'
    }
    

    Oh, and to remove an object:

    delete ajaxCache['item' + id];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store data from a xml-file in a associative Array. Creating the
How to store the received the data from an remote php file in an
I want to store the data received from serial port in a string variable
I'm facing a problems here. This is the data that I've received from my
i am trying to store data from the app engine app to google cloud
I have a table that holds data received from a thirdparty via an api
I'm building an android app who needs to present lots of data received from
I want to populate the combobox dynamically based on the data I received from
I am building an application that has to display data received from an external
I am trying to start a PHP session to store data from a form

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.