0

I've got an object that I get from a sql query and I want to delete an item inside of it. The thing is that after I delete it, that item has no information but it's still there as:

<1 empty item>

so I would like to know if there is a way to completely remove it and have a clean object with only my data." The code is to establish matches between two players from the database and it used to work but I would have to verify that the selected player was not the one being left out since they are odd and I wanted a random one to be left out. So I realized it was way easier to simply remove the player that is not going to take part in the matches from the object. I will leave the hole code.

    let tournamentID = args[0];
    let categoryID = args[1];
    let tournamentSQL = 'SELECT * FROM tournaments WHERE tournamentID = ?';
    let tournamentData = [tournamentID];
    let matchesCreated = 0;
    con.query(tournamentSQL, tournamentData, function(err, result){
        if(err) throw err;
        let playersSQL = "SELECT * FROM players WHERE tournamentID = ?";
        if(result.length == 0){
            return message.channel.send('Ingresaste un TournamentID incorrecto');
        };
        if (result[0].modality > 1){
            return message.channel.send('Este torneo es por equipos, usa .partidosequipos');
        };
        let actualRound = result[0].actualRound + 1;
        con.query(playersSQL, tournamentData, function(err, resultPlayers){
            if(resultPlayers.length == 0){
                return message.channel.send('Este torneo no tiene jugadores.');
            };
            if(err) throw err;
            let roundPlayers = resultPlayers.length - 1;
            if(resultPlayers.length % 2 != 0){
                let player = Math.round(Math.random() * roundPlayers);
                console.log(player);
                message.channel.send(`La cantidad de jugadores en el torneo es impar, el jugador ${resultPlayers[player]} no jugará en esta ronda y ya clasificó a la siguiente`);
                delete resultPlayers[player];
                matchCreating(roundPlayers, resultPlayers, result, categoryID, client, message, actualRound);
            } else{
                matchCreating(roundPlayers, resultPlayers, result, categoryID, client, message, actualRound);
            }

Hope I was able to explain my self. Thank you for your help.

5
  • 3
    Please attach your code and data from sql query. Commented Oct 5, 2020 at 19:02
  • Okay, thank you for your answer. Done. Commented Oct 5, 2020 at 19:06
  • Can you describe your question in more detail? Attach the part of query result and what you want really and what have you done so far for that. Commented Oct 5, 2020 at 19:09
  • What sql library are you using? And is resultPlayers an array? Commented Oct 5, 2020 at 19:13
  • Hey guys, I left my hole code there. Commented Oct 5, 2020 at 19:14

1 Answer 1

2

You need to use splice to delete a particular index

var playersSQL = "SELECT * FROM players WHERE tournamentID = ?";
con.query(playersSQL, tournamentData, function(err, resultPlayers){
    if(resultPlayers.length % 2 != 0){
        let player = Math.round(Math.random() * roundPlayers);
        resultPlayers.splice(player, 1);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.