0

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)
Vardan Betikyan
  • 354
  • 5
  • 20
  • Use Map and not array for the usecase. You can't do PLAYER_LIST["3oPNoqkvaBtAYPGrAAAr"] with arrays – HiRenS Apr 30 '21 at 04:50
  • Can you provide a bit more context? What are you trying to achieve? Are you trying to create a new player if any of the players in your list doesn't contain an id? – Jim Vercoelen Apr 30 '21 at 04:51
  • @HiRenS always seemed to work before :/ also, this example states it can be done https://stackoverflow.com/questions/4090491/how-to-get-the-first-element-of-an-array – Vardan Betikyan Apr 30 '21 at 05:00
  • @JimVercoelen if player doesn't exist, new player is put into the array along with it's data (obj). If player exists, simply just update the position, angle, controls, ect – Vardan Betikyan Apr 30 '21 at 05:01
  • @VardanBetikyan I updated my answer to match your use case(?). – Jim Vercoelen Apr 30 '21 at 05:23
  • @JimVercoelen I was cleaning out my code and this issue happened, I'm not sure why it worked before. Albeit I went around it with a simpler different method that matched my code. I very much appreciate the thought and effort you put into this! – Vardan Betikyan Apr 30 '21 at 05:29

1 Answers1

1

Your list is an array, not an object, so you won't be able to get the player from the list using players['player-id']

You don't need to iterate over the entire list, just simply detect whether or not the player exists, when that's not the case: create one and add it to your list, otherwise update the existing player in the list with the new player data.

Try something like this:

<!DOCTYPE html>
    <body>
        <script>
            var PLAYER_LIST = [{
                id: 1,
                name: 'john do'
            }];

            function createPlayer(newPlayer) {
                // what ever it is you do here..
                return newPlayer;
            }

            function refresh(data) {
                const playerIndex = PLAYER_LIST.findIndex(p => p.id === data.id);

                if (playerIndex === -1) {
                    const newPlayer = createPlayer(data);
                    PLAYER_LIST.push(newPlayer);
                } else {
                    PLAYER_LIST[playerIndex] = data;
                }
            }

            refresh({ name: 'jane do' }); // I don't exist, create me
            refresh({ id: 1, name: 'changed' }); // I exist, update me
            console.log('Refreshed list: ', PLAYER_LIST);
        </script>
    </body>
</html>
Jim Vercoelen
  • 1,048
  • 2
  • 14
  • 40