1

i have little trouble in populating an existing array with json returned data. Here is what i have

myarr=[];
function fillarr()
    {
    $.getJSON("test.php?c=10",function(data)
            {
            $.each(data, function(key, val)
                    {
                    myarr.push(data[val]);
                    }
                    });
            });
    }

My Problem is, that the array is emty outside the functions. Please help.

1
  • what does the return data look like? is it an object literal or an array? and what is the scope of myarr? Commented Apr 19, 2012 at 8:41

3 Answers 3

1
myarr=[];
function fillarr()
    {
    $.getJSON("test.php?c=10",function(data)
            {
            $.each(data, function(key, val)
                    {
                        myarr.push(val);
                        console.log(myarr); // you will myarr here, not out side
                    }
                    });
            });
      console.log(myarr); // wont get
    }

myarr get its content after ajax request complete and its take time. so console outside of $.getJSON execute before request complete.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, but it makes no diffrence. The values are only available in the function and not outside the function.
1
myarr=[];
function fillarr()
{
    $.getJSON("test.php?c=10", function(data) {
        $.each(data, function(key, val) {
            myarr.push(val);
        });
        doSomethingNowThatTheArrayIsActuallyPopulated();
    });
}

fillarr();

console.log(myarr); // This will print an empty array, it hasn't been populated yet.

function doSomethingNowThatTheArrayIsActuallyPopulated() {
    console.log(myarr); // This will print the array which now contains the json values
}

1 Comment

The returned data is an array.
0

if the return data is an object, it's easier to do jQuery.each to push each value to the array.

function fillarr(){
    $.getJSON("test.php?c=10",function(data){
        $.each(data, function(key, val){
            myarr.push(val);
        });
    });
}

if the return data is an array, Array concat would be faster

function fillarr(){
    $.getJSON("test.php?c=10",function(data){
        myarr = myarr.concat(data);
    });
}

6 Comments

the returned data is an array.
then concat would be better for the job
I've tried all your suggestion, thank you. the are working, but only inside the function. outside the function , the arra is still empty.
how can it be empty? where is this array declared?
That's because it hasn't fetched and processed the JSON yet, so the array is still empty. You need to use the getJSON callback to know when you can use the array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.