javascript - jQuery - getJSON data to array - Stack Overflow

How do you store the data received from jQuery getJSON to an array for later usage? Here's a sampl

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('.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]);
    });
})
​

/

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

Share Improve this question edited Aug 9, 2010 at 8:20 ina asked Aug 9, 2010 at 8:00 inaina 19.6k41 gold badges127 silver badges209 bronze badges 2
  • 1 If it's not an array already, what does it look like? – Álvaro González Commented Aug 9, 2010 at 8:01
  • I'm not able to reference any of the elements outside of the getJSON callback. I've tried defining an array=data[i] (for loop, i) inside the callback, but the array reads undefined outside of the callback – ina Commented Aug 9, 2010 at 8:03
Add a ment  | 

3 Answers 3

Reset to default 2

take a look at this: How can I return a variable from a $.getJSON function

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];

just make sure that the JSON you receive is an array. Store this JSON array into array and use whenever you want.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745363621a4624473.html

相关推荐

  • javascript - jQuery - getJSON data to array - Stack Overflow

    How do you store the data received from jQuery getJSON to an array for later usage? Here's a sampl

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信