How to use a variable as an array?

1

I have the following code that randomly generates an alphanumeric string:

var currentGameID = Math.random().toString(36).slice(2);

Then, in the following code, take the names of the players from the text boxes and store them in a new object, staying as follows:

$(document).ready(function() {
        $("#newGame").click(function() {
            player1Name = $("#jugador1").val();
            player2Name = $("#jugador2").val();
            gamesSessions = {
                sessionID: {
                    gameID: {
                        player1Name: player1Name,
                        player2Name: player2Name
                    }
                }
            };
        });
    });

The output is as follows:

What I need to know is how I can make the variable "currentGameID" be the array that contains "player1Name" and "player2Name", so that it could save the player data inside each "currentGameID" .

The complete code is as follows:

var currentGameID = Math.random().toString(36).slice(2);
    console.log(currentGameID);
    var player1Name;
    var player2Name;
    var gamesSessions;
    $(document).ready(function() {
        $("#newGame").click(function() {
            player1Name = $("#jugador1").val();
            player2Name = $("#jugador2").val();
            gamesSessions = {
                sessionID: {
                    gameID: {
                        player1Name: player1Name,
                        player2Name: player2Name
                    }
                }
            };
        });
    });
    
asked by matias 03.07.2018 в 00:48
source

2 answers

0

If I understand you well, what you need is that currentGameID is an object and not a string. You could do it like this:

        var currentGameID = {'id': Math.random().toString(36).slice(2)};
        
        var player1Name;
        var player2Name;
        var gamesSessions;
        $(document).ready(function() {
            $("#newGame").click(function() {
                player1Name = $("#jugador1").val();
                player2Name = $("#jugador2").val();
                gamesSessions = {
                    "sessionID": {
                        "gameID": {
                            player1Name: player1Name,
                            player2Name: player2Name
                        }
                    }
                };
                currentGameID.jugadores = gamesSessions.sessionID.gameID;
                console.log(currentGameID);
            });
        });            
       
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" id="jugador1"></input>
<input type="text" id="jugador2"></input>
<button id="newGame">Jugar</button>
    
answered by 03.07.2018 / 01:08
source
1

If you want to save each of the games in an array you can do the following:

var currentGameID=[];
$(document).ready(function() {
    $("#newGame").click(function() {
            currentGameID.push({
                gameId:Math.random().toString(36).slice(2),
                gamesSessions:{
                    gameID: {
                        player1Name: $("#jugador1").val(),
                        player2Name: $("#jugador2").val()
                    }
                }
            });
            console.log(currentGameID);

    });
});

The code may have errors because I have not tried it, but what it does is use the push function to add a new position to the array every time you click on #newGame.

You may also have to change the structure of your object according to your needs and finally you have to verify that the chain generated by Math.random().toString(36).slice(2) no longer exists in another game

    
answered by 03.07.2018 в 01:14