Result of arr[0].price is 'undefined' outside of the function. Why is the variable arr not available when it is defined outside of the function and data is pushed to it?
var arr = [];
function get_data() {
$.ajax({
url: 'http://bitcoinprice.co.uk/site-options/',
dataType: 'json',
success: function(data) {
var price = data['gbp_price'];
var abs_change = data['gbp_abs_change'];
var p_change = data['gbp_change'];
var market_cap = data['gbp_market_cap'];
var today_max = data['gbp_today_max'];
var today_min = data['gbp_today_min'];
var obj = {
'price' : 50,
'abs_change' : abs_change,
'p_change' : p_change,
'market_cap' : market_cap,
'max' : today_max,
'min' : today_min
};
arr.push(obj);
console.log(arr[0].price); // 50
}
});
}
get_data();
console.log(arr[0].price) // Uncaught TypeError: Cannot read property 'price' of undefined(…)
I can't seem to find an answer to this. I am trying to get the value from the key 'price' outside of the function get_data().
I'm sure I'm missing something simple here.