0

I have a global variable say :

var series,hours;
var loadData = function(){
series = [[]];
hours = [];
d3.json("data/radar.json", function(error, data) {

    data.QualitySummaryObject.forEach(function(d,i) {
        series[i] = d.extractPercentage;
        hours[i] = d.extractorName;
  });


});  
console.log(hours);
};

Now if I am trying to access this console.log its working fine, but.

var print = function(){
    console.log(hours); //here its not working no value in hours why ... ?
}
VisioN
  • 138,460
  • 30
  • 271
  • 271
Saurabh Sinha
  • 1,722
  • 4
  • 27
  • 53

2 Answers2

2

Ensure that you hours is global:

window.hours = [];

Then anywhere you can log it:

console.log(window.hours);

Using directly var without declaration will avoid context problems.

Gabriel Gartz
  • 2,750
  • 21
  • 24
0

If you are running the above code in a function, the variable hours is not a global variable because you previously declared it with var.

If you want to declare a variable as a global variable, instead of this:

var hours = [];

Do this:

hours = [];

To declare a global variable, all you have to do is give it a name.

starbeamrainbowlabs
  • 5,236
  • 7
  • 41
  • 71