Take the 2-minute tour ×

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){


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

share|improve this question
do you look at this Solution on SO.. stackoverflow.com/questions/1739800/… –  Usman Sep 24 '12 at 4:36

1 Answer 1

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.

share|improve this answer

Your Answer


