persistence in LocalStorage (fixes)

1

I have the following code, what I want is to save an array in my localStorage, which will be saved 1 by one as I add them, so far this is what I have, if you save the first item in the room, but already from the second, it begins to over write in the same position. Greetings Colleagues

function datos(idx,model){
    
    var fanIdx  = new Array();
    var fanModel = new Array();


    var datos  = [];
    var objeto = {};

var datosLocal = localStorage.getItem('datos');
datosArray = JSON.parse(datosLocal);

if(datosLocal != null) {
for(var i = 0; i < datosArray.length; i++) {
    //agregamos el item después del que ya se a agregado
   datosArray.push({ 
        "fanIdx"    : idx,
        "fanModel"  : model 
    });
  }
  
}else{
   //agregamos el primer item
   datos.push({ 
        "fanIdx"    : idx,
        "fanModel"  : model 
        });
    
}
    
objeto.datos = datos;

console.log(datosArray)
localStorage.setItem("datos", JSON.stringify(objeto));
//console.log(JSON.stringify(objeto));

}

$(document).ready(function() {
datos(0,'5SILENT100');
 //datos(1,'5SILENT200');
 //datos(2,'5SILENT300');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
asked by Manueel Perezz 26.12.2018 в 23:59
source

1 answer

1

The variable datos of localStorage is finally as follows. It has the property datos , which is an array of objects, in each object it has the properties fanIdx , which is an integer, and fanModel which is a String :

{
    "datos": [
        {"fanIdx":0,"fanModel":"5SILENT100"},
        {"fanIdx":1,"fanModel":"5SILENT200"},
        {"fanIdx":2,"fanModel":"5SILENT300"}
    ]
}

That is, the variables fanIdx and fanModel are not arrays, but are variables of primitive type. It is also not necessary to declare these variables because they can be assigned directly to the object.

Use a ternary operator to assign a value to the object. If the variable exists in localStorage , I assign the values of the variable, otherwise, I assign a default object, without values.

That is:

var objeto = si pasa esto ? hago esto : si no, hago esto otro

For this example it is:

var objeto = datosLocal==null?{datos:[]}:JSON.parse(datosLocal)

Then I do push , whatever the previous case, that is, what was previously inside the if and also inside the else , being from both sides makes it always run, then what step outside the conditional structure as a single push , immediately after declaring the objeto with its values.

Finally, I save the object%% co.

Code:

function datos(idx,model) {

    var datosLocal = localStorage.getItem('datos')
    var objeto = datosLocal==null?{datos:[]}:JSON.parse(datosLocal)
    objeto.datos.push({"fanIdx": idx,"fanModel": model})
    localStorage.setItem("datos", JSON.stringify(objeto))
    console.log(localStorage.getItem('datos'))
}
datos(0,'5SILENT100')
datos(1,'5SILENT200')
datos(2,'5SILENT300')
    
answered by 27.12.2018 / 01:52
source