I have a weird situation, where array[0]
is returning Undefined
, even if there are elements in the array.
Any ideas?
var PLAYER_LIST = [];
function refresh(data){
var players = data.players;
for(var p in players){
var newPlayer = players[p];
var id = newPlayer.id;
if(PLAYER_LIST[id] == undefined){
PLAYER_LIST[id] = createPlayer(newPlayer);
}
var player = PLAYER_LIST[id];
player.position = newPlayer.position;
player.angle = newPlayer.angle;
player.controls = newPlayer.controls;
player.speed = newPlayer.speed;
player.update = 0;
}
console.log(PLAYER_LIST[0]); //returns Undefined
console.log(PLAYER_LIST); //returns entire array (works normally)
console.log(PLAYER_LIST.length); //returns 0 (when it should return 1)
}
refresh(obj); //obj full of new player info
console.log(PLAYER_LIST)
returns
[3oPNoqkvaBtAYPGrAAAr: {…}]
3oPNoqkvaBtAYPGrAAAr: {id: "3oPNoqkvaBtAYPGrAAAr", animation: 0,
animationCountTotal: 5, animationCount: 4, saveAngle: 0, …}
length: 0
__proto__: Array(0)