I need some help with my code (clearly). So far I’m able to get the json data that I want but I can’t seem to store it in a global array. My goal is to get data.count and data.url in an array like this:
var myHelpfulVariable = [
// e.g. [data.count, '{label:' + data.url + '}']
[3856, {label:'http://www.google.com/'}],
[1897, {label:'http://www.yahoo.com/'}],
[3870, {label:'http://www.microsoft.com/'}]
]
I’ve tried a few different methods using the push function all to no avail. Below is the code that gets the data but it doesn’t attempt to put it into an array.
Could someone please help? An explanation of why it works would also be helpful. Thanks in advance!
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
var getData = [
'http://urls.api.twitter.com/1/urls/count.json?url=http://www.google.com&callback=?',
'http://urls.api.twitter.com/1/urls/count.json?url=http://www.yahoo.com/&callback=?',
'http://urls.api.twitter.com/1/urls/count.json?url=http://www.microsoft.com&callback=?'
];
$.each(getData, function(i,getValue) {
var urlForGetJSONfunction = "'" + getValue + "'"
$.getJSON(getValue, function(data) {
$('<div></div>').append('<strong>' + data.count + '</strong>' + ' retweets for: ' + '<a href="' + data.url + '">' + data.url + '</a>').appendTo('body')
})//ends getJSON
})//ends each loop
});
</script>
array.push( obj )does exactly what you would expect, adds the object to the end of the array. Arrays elements in JS don’t have to be of the same type, so you can simply push your data as you get it as follows: