0

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.

8
  • 4
    try arr[0].price Commented Nov 26, 2016 at 13:32
  • 1
    Javascript has no associative arrays, only index-based arrays. Commented Nov 26, 2016 at 13:37
  • Returns "Cannot read property 'price' of undefined(…)" Commented Nov 26, 2016 at 13:43
  • 2
    Because it is not loaded yet! your get_data() function did not the complete the request. so your arr is still empty Commented Nov 26, 2016 at 14:15
  • 1
    stackoverflow.com/questions/23667086/… Commented Nov 26, 2016 at 14:20

3 Answers 3

4

You need to use arr[0].price, because the price property is defined not on array, but on the object, that is the first entry in the array.

You code can be rewritten like that to demonstrate what's happening:

var o = {'price':price};
o.price; // 50
arr.push(o);
arr[0] === o; // true

You would have to do like this to actually assign a property to the array:

arr.price = price;
Sign up to request clarification or add additional context in comments.

4 Comments

Ignore, your code works, as does mine. I simplified my issue and it eliminated some scope issues that are causing the problem. Thank you for your answer.
@Nick, glad it helped. Good luck
rather than creating a new question I've updated it to show the scoping issues.
@Nick, don't forget, you can accept my answer if gives you correct explanation
0

Just another option

var price = 50,
        arr = [];
arr.price = price;

console.log(arr.price+'<br>'+arr['price']); // both return 50

1 Comment

I've updated my question to show the scoping issues.
0

var arr is none other than array of objects. You just needed to loop in the array to get value.

var price = 50;
var arr = [];

var obj = {
    'price' : price
}

arr.push(obj);

for(var i of arr)
    {
    console.log(i['price']); //50
    }

2 Comments

I've updated my question to show the scoping issues.
Are you sure there is a scoping issue in your question? I get output 50 out of the block too!

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.