0

I am so stuck with access/store data outside getJSON

var arr = new Array();  // an array to store result

$.getJSON(url, function(data) {
    $.each(data.data, function(index, value){
    arr.push(value);
    }

 }

need to access arr here.

I tried using $ajax, async:false(as this post indicate Variables set during $.getJSON function only accessible within function), but still no luck. I know this question has been asked many times and I did some search, but still havent make it work. Tons of thanks

Community
  • 1
  • 1
Yuan Vivien Wang
  • 35
  • 1
  • 1
  • 4
  • do you look at this Solution on SO.. http://stackoverflow.com/questions/1739800/variables-set-during-getjson-function-only-accessible-within-function?lq=1 – Usman Sep 24 '12 at 04:36

1 Answers1

0

Why not simply:

var arr = [];
$.getJSON(url, function(data) {
   arr = data;
});

You're taking the array produced by the JSON decode, and then assigning its values in a loop. you could just assign the whole array at once, preserving the keys from the original json array as is. Your code is preserving only values.

Marc B
  • 356,200
  • 43
  • 426
  • 500