user2567857
user2567857

Reputation: 483

Display stored array elements of localstorage on web page

I am able to successfully store the values of localStorage and display it in console.log(). As, the values are stored in an array, so I am not able to display them in my page with document.write. I want the stored values to be displayed on page. Can anybody suggest a way to do it? My code is:

// List of all entries
console.log(localStorage.getItem("total"));
// Last entry inserted
console.log(localStorage.getItem("intotal"));

document.write(total + "<br>"); // showing in console.log -> "Uncaught ReferenceError: allEntries is not defined "
document.write(intotal + "<br>"); // showing [object Object]

I don't want to display elements as:

 for(i=0; i< localStorage.length -1; i++)

As, I have many localStorage key/value pairs and I don't want to display all localStorage. By this I mean, I am also storing other values in different key/pair values, like:

 localStorage.setItem("Mymarks", (JSON.stringify(marks)); // where marks is JSON object
 localStorage.getItem("Mymarks");

So, if I use the for loop, then Mymarks/marks key/pair value is also getting displayed on my page.

My desired output is same as you can see in console.log. Like:

[{"name":"john","place":"asn"},"text":"abcd"},{"title":{"name":"john","place":"asn"},"text":"abcd"}] 

  {"name":"john","place":"asn"},"text":"abcd"}

Upvotes: 1

Views: 2258

Answers (1)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

Well, we don't know exactly how you want to display them, but this would be a way:

EDIT

document.write("<pre>" + JSON.stringify(existingEntries) + "</pre>");    
document.write("<pre>" + JSON.stringify(entry) + "</pre>");

Cheers, from La Paz, Bolivia

Upvotes: 2

Related Questions