So I have an array that I use .push()
in a function to add to the array, but if I try to console.log
the array outside of the function it shows the array as empty. I have the array declared as the very first line of code so every function can access it, but anything outside of the function I changed it in just shows the array as blank. Is there any way I can get the contents of the array from another function? Any help would be appreciated :)
I'm using JavaScript/jQuery if that helps at all.
Here's the code:
var id64List = [];
<other functions>
function handle(e) {
if (e.keyCode === 13) {
e.preventDefault();
var searchBox = document.getElementById("search").value;
var isnum = /^\d+$/.test(searchBox);
if (searchBox.length == 17 && isnum === true) {
useAPI();
} else {
alert("Not a valid steam url!");
}
}
}
function useAPI() {
var friendsRequest = new XMLHttpRequest();
friendsRequest.open("GET", "<api im using>/friends/id64/" + id64);
friendsRequest.onreadystatechange = function () {
if (friendsRequest.readyState === 4) {
var friendsResponse = JSON.parse(friendsRequest.responseText);
var wrapperDiv = document.getElementById('friends-body');
var count = 0;
var friendFilter = 1;
var friendFilter2 = 0;
var arrayLength = friendsResponse.friendslist.friends.length;
for (var i = 0; i < arrayLength; i++) {
count++;
friendFilter = friendFilter + 2;
friendFilter2 = friendFilter2 + 2;
var friendDiv1 = document.createElement('div');
var friendDiv2 = document.createElement('div');
if (count < 9) {
friendDiv1.id = "friendDiv";
friendDiv1.innerHTML = "<div id='friendProfilePicture'></div>" + friendsResponse.friendslist.friends[friendFilter].steamid;
wrapperDiv.appendChild(friendDiv1);
id64List.push(friendsResponse.friendslist.friends[friendFilter].steamid);
friendDiv2.id = "friendDiv2";
friendDiv2.innerHTML = "<div id='friendProfilePicture'></div>" + friendsResponse.friendslist.friends[friendFilter2].steamid;
wrapperDiv.appendChild(friendDiv2);
id64List.push(friendsResponse.friendslist.friends[friendFilter2].steamid);
} else {
break;
}
}
document.body.appendChild(wrapperDiv);
}
};
friendsRequest.send();
alert(id64List);
}
In the alert at the very bottom it just shows a blank space.