Search code examples
javascriptjqueryjsonajaxgetjson

Global access to array values made from Json


$.getJSON("sluzba.json", function(result){
    array = $.each(result, function(value){
        return value;
    });
  tableMaker(array);
});

This is my code, I want to have an access to the array from outside the scope of this function.

Is it possible?

Please help...


Solution

  • Yes, it is possible. You can using a callback method.

    function myFunction(callback){
        $.getJSON("sluzba.json", function(result){
        array = $.each(result, function(value){
           return value;
        });
        callback(array);
        tableMaker(array);
        });
    }
    myFunction(function(myArray){
       console.log(myArray);
    });