Home > front end >  How read a Object into localstorage javascript
How read a Object into localstorage javascript

Time:08-16

now i save an array in my localStorage, and works fine but how get atribute by attribute. How write the cicle for?

localStorage.setItem('bultosStorage')

[{IdBulto: 9407981, EstadoEntrega: "A", Bulto: "025602", Contenedor: "025602"}]
0: {IdBulto: 9407981, EstadoEntrega: "A", Bulto: "025602", Contenedor: "025602"}
1: {IdBulto: 9407980, EstadoEntrega: "A", Bulto: "025604", Contenedor: "025603"

alocalStorage = localStorage.getItem('bultosStorage');

How i get IdBulto, Bulto, Contenedor, and before add a new node a JSON.

I have json :

"ControlEntrega": 107252, "IdEstadoDespacho": 1, "IdEstadoRetirado": 2, "IdEstadoCobro": 2, "IdEstadoEntregaFallidas": 0, "IdEstadoGaveta": 2, "CodigoLocalidad": 1, "UsuarioActualizacion": "Paul.Rizzo", "IpActualizacion": "LiquidacionEntrega", "Proceso": "E", "Detalles": [ { "ControlEntregaDetalle": 3774286, "ControlEntrega": 107252, bultosEntrega": [ { "idBulto": 9407982, "contenedor": "010168", "bulto": "010168", "estado": "A" } ] } ]

Bultos Entrega most add whith the data for. Thanks

CodePudding user response:

You are storing the array as a string, and so you can parse it to JSON when you get it back from the local storage

var arr = [{ IdBulto: 9407981, EstadoEntrega: "A", Bulto: "025602", Contenedor: "025602" },
{ IdBulto: 9407981, EstadoEntrega: "A", Bulto: "025602", Contenedor: "025602" },
{ IdBulto: 9407980, EstadoEntrega: "A", Bulto: "025604", Contenedor: "025603" }]

// Store your array
localStorage.setItem('bultosStorage', JSON.stringify(arr));

// Retrieve your array
var obj = JSON.parse(localStorage.getItem('bultosStorage'));

// Access your array elements
for (let element of obj) {
    console.log(element.IdBulto, element.Contenedor, element.Bulto, element.EstadoEntrega)
}

CodePudding user response:

alocalStorage = JSON.parse(localStorage.getItem('bultosStorage'));

when you set object in localstorage use:

localStorage.setItem('object', JSON.stringify(object))
  • Related