Verify that a value exists in a JSON localStorage

0

I want you to click on a button first to verify if the value exists in a JSON that you get from localStorage and if it does not exist, save it, but when you perform the function even if the value exists, continue adding the array. The function is:

$('#btnNP').click(function(){
        if ($.trim($('#txtplayer').val())==''){         
            alert('Ingresa tu nombre de jugador');
            $('#txtpalabra').focus();
            return false;
        }

        var jugador =$('#txtplayer').val();

        for(var i in Jugadores)
        {
            var con = JSON.parse(Jugadores[i]);
            if(con[i].player === j){
            console.log(con[i].player); 
            }
            if(con.player == jugador){
                console.log(con.player);
            }
            else{
                var jugador_push = JSON.stringify({
                    "player" : jugador,
                    "score" : 0
                });
                Jugadores.push(jugador_push);
                localStorage.setItem("Jugadores", JSON.stringify(Jugadores));
            }
        }
    });
    
asked by Carlos Roberto Luna Ochoa 08.06.2018 в 18:01
source

1 answer

0

Well, sorry for the delay, Fiddle did not let me share it, so I put it on a CodePen :

It looks like this:

// find elements
var button = $("button")

window.localStorage.setItem("Jugadores","[]"); 

// handle click and add class
button.on("click", function(){
  	if ($.trim($('#txtplayer').val())==''){         
            alert('Ingresa tu nombre de jugador');
            $('#txtpalabra').focus();
            return false;
        }

        var jugador =$('#txtplayer').val();
        var Jugadores = localStorage.getItem("Jugadores"); 
        Jugadores = JSON.parse(Jugadores);
        
        if(Jugadores.length == 0)
        {
        	var jugador_push = JSON.stringify({
                    "player" : jugador,
                    "score" : 0
                });
                Jugadores.push(jugador_push);
                localStorage.setItem("Jugadores", JSON.stringify(Jugadores));
        }
        else
        {
            var yaExiste = false;

            for(var i in Jugadores)
            {
                var con = JSON.parse(Jugadores[i]);
                //if(con[i].player === j){
                  //console.log(con[i].player); 
                //}
                if(con.player == jugador){
                    yaExiste = true;
                }
            }
            
            if(yaExiste)
            {
               console.log(con.player);
            }
            else
            {
                var jugador_push = JSON.stringify({
                        "player" : jugador,
                        "score" : 0
                 });
                 Jugadores.push(jugador_push);
                 localStorage.setItem("Jugadores", JSON.stringify(Jugadores));
            }
        }

        console.log(localStorage.getItem("Jugadores"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="banner-message">
  <p>Jugadores</p>
  <input id="txtplayer" value=""/>
  <button>Agregar</button>
</div>

The problem is that when you do the FOR, you go through the entire array, and eventually find one that does not match, then add it. Using a flag yaExiste = false It allows us to review the arrangement and since we saw that it already exists, we do not add it, and if not, add it. I hope this clear, if any doubt warn me:)

** The code does not work here for the localstorage, I leave you the link of the Codepen

    
answered by 08.06.2018 в 18:44