I want to store some data as properties inside the "randomImages" object (this randomImages object will be used in another function later) inside a jquery foreach "loop". But I find that the data is missing after the loop finishes (inside the ajax callback, the data is there). It seems like some sort of scope or context issue but I don't know what is wrong. The exact code I am using is below.
var randomImages = {};
$.get('http://pixabay.com/api/?username=bhinbox0&key=8792f764c13d09a7b7d2&q=yellow+flower&image_type=photo', function (data) {
$.each(data.hits, function (index, entry) {
randomImages[entry.webformatURL] = 'center';
});
//randomImages.toSource() shows a lot of data here
});
$('p').text(randomImages.toSource()); //why is randomImages empty here?