Storing Objects in localStorage

store object in localstorage angular 6
can we store javascript objects directly into localstorage
how to store multiple objects in localstorage
clear localstorage
store array of objects in localstorage
how to store data in localstorage in javascript
store array in localstorage
localstorage setitem

I have an array like this:

[{name:"test", time:"Date 2017-02-03T08:38:04.449Z"}]

I stored it in localstorage and when I retrieving data from localstorage I got the value:

[object, object]

How can I solve this issue?


import { Injectable } from "@angular/core";

export class TokenManager {

  public tokenKey: string = 'app_token';

  constructor() { }    

  store(content) {
    var contentData;

    console.log("inside localstorsge store:", content);
    contentData =
      (data) =>
    console.log("contentData:", contentData)
    localStorage.setItem(this.tokenKey, content);

  retrieve() {
    console.log("inside localstorage");
    let storedToken: any = localStorage.getItem(this.tokenKey);
    console.log("storedToken:", storedToken);//====> here this console is[object object]
    if (!storedToken) throw 'no token found';
    return storedToken;


local storage limited to handle only string key/value pairs you can do like below using JSON.stringify and while getting value JSON.parse

var testObject ={name:"test", time:"Date 2017-02-03T08:38:04.449Z"};

Put the object into storage:

localStorage.setItem('testObject', JSON.stringify(testObject));

Retrieve the object from storage:

var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

Storing Objects in HTML5 localStorage, The limitations of using Local Storage in JavaScript is that you are only able to store data as Duration: 6:58 Posted: 20 Feb 2018 There are libraries that polyfill that API, based on localStorage, that make it available everywhere. Summary. Web storage objects localStorage and sessionStorage allow to store key/value in the browser. Both key and value must be strings. The limit is 5mb+, depends on the browser. They do not expire. The data is bound to the origin (domain

it is a little late for answering, but I want to answer until future viewers can use it, javascript use objects by reference, then when we store an object to localStorage, in real we store the address of object , not the content of object! then if we want to store object content (absolutely we want), we should do like below:

store like this:

localStorage.setItem('my_item', JSON.stringify(my_object));   

and use like this:

var my_object = JSON.parse(localStorage.getItem('my_item'));

hope to be helpful :)

Storing objects with Local Storage in JavaScript, Local storage can only save strings, so storing objects requires that they be turned into strings using JSON.stringify - you can't ask local storage to store an� localStorage stores key-value pairs. So to store a entire javascript object we need to serialize it first (with JSON.stringify, for example): localStorage.setItem('user', JSON.stringify(user)); Then to retrieve it from the store and convert to an object again: var user = JSON.parse(localStorage.getItem('user'));

local storage is only capable to handle string key/value pairs. If you want to store json objects use JSON.stringify() and while fetching value from local storage use JSON.parse()

Example of object to store:

var storeObject={name:"TestDate", time:"Date 2020-01-21T08:38:04.449Z"};

way to store:

localStorage.setItem('storeObject', JSON.stringify(storeObject));

way to fetch:

var fetchedObject = localStorage.getItem('storeObject');
console.log('fetchedObject for local storage: ', JSON.parse(fetchedObject ));

Local Storage, To store arrays or objects, you would have to convert them to strings. To do this, we use the JSON.stringify() method before passing to setItem� localStorage is a type of web storage that allows JavaScript sites and apps to store and access data right in the browser with no expiration date. This means the data stored in the browser will persist even after the browser window has been closed.

You cannot store something without String Format.

LocalStorage always store key and value in string format.

That is why you should convert your data to string whatever it is Array or Object.

To Store data in localStorage first of all stringify it using JSON.stringify() method.

var myObj = [{name:"test", time:"Date 2017-02-03T08:38:04.449Z"}];
localStorage.setItem('item', JSON.stringify(myObj));

Then when you want to retrieve data , you need to parse the String to Object again.

var getObj = JSON.parse(localStorage.getItem('item'));

The complete guide to using localStorage in JavaScript apps , Web storage provides two new objects for storing data on the client: localStorage – stores data with no expiration date. sessionStorage – stores� I have written a blog recently on sessionStorage and localStorage. However few questions I received on that blog is how can we store Javascript objects in sessionStorage and localStorage. Storing simple string and integer values are fine but what if you want to store a JavaScript object and get it back. Let’s try this simple code.

It's easy to store objects in local storage with localDataStorage, where you can transparently set/get any of the following "types": Array, Boolean, Date, Float, Integer, Null, Object or String.

[DISCLAIMER] I am the author of the utility [/DISCLAIMER]


localDataStorage.set( 'key1', 'Belgian' )
localDataStorage.set( 'key2', 1200.0047 )
localDataStorage.set( 'key3', true )
localDataStorage.set( 'key4', { 'RSK' : [1,'3',5,'7',9] } )
localDataStorage.set( 'key5', null )

localDataStorage.get( 'key1' )   -->   'Belgian'
localDataStorage.get( 'key2' )   -->   1200.0047
localDataStorage.get( 'key3' )   -->   true
localDataStorage.get( 'key4' )   -->   Object {RSK: Array(5)}
localDataStorage.get( 'key5' )   -->   null

As you can see, the primitive values are respected. Now, in your case, we would do this:

>localDataStorage.set( 'testObject', { name : 'test', time : new Date( '2017-02-03T08:38:04.449Z' ) } )

Note that you can plainly express the object. (All the stringamication is done in the background for you.) When we retreive the key, we get:

>localDataStorage.get( 'testObject' ) -->
>Object {name: "test", time: "2017-02-03T08:38:04.449Z"}

Storing JSON Objects in HTML5 Local Storage – Shravan Weblog, (As with objects, integer keys are automatically converted to strings.) Syntax. myStorage = window.localStorage;. Value. A Storage object which� HTML Web Storage Objects. HTML web storage provides two objects for storing data on the client: window.localStorage - stores data with no expiration date; window.sessionStorage - stores data for one session (data is lost when the browser tab is closed) Before using web storage, check browser support for localStorage and sessionStorage:

Window.localStorage, To correctly store JavaScript objects in LocalStorage, we first need to convert our object to a JSON string. We use the built-in JSON.stringify() function for this. The� Now that we have covered all the functions to manage data in the user's browser, let's look at the special case of saving complex objects instead of string data. Storing Objects in LocalStorage. LocalStorage can only use strings for its keys and values. If we try to store any other type of data, it converts it to a string before storing it.

Storing Data in the Browser with LocalStorage, Answer: Use the JSON.stringify() Method. By default, the localStorage or sessionStorage only allows you to store string key/value pairs. But you can also store the� The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.

How to Store JavaScript Objects in HTML5 localStorage, Note that localStorage supports only keys and values that are string. To extend the solution for objects, you can stringify the object before saving in local storage, � Questions: I’d like to store a JavaScript object in HTML5 localStorage, but my object is apparently being converted to a string. I can store and retrieve primitive JavaScript types and arrays using localStorage, but objects don’t seem to work.

  • you tried this JSON.stringify(storedToken) ?
  • JSON.stringyfy the object before saving to localstorage and parse it back while retrieving with JSON.parse.
  • @Satpal what is OP ?
  • Possible duplicate of Storing Objects in HTML5 localStorage
  • see this :…
  • it works fine thanks for your reply. but while mapping this i got each as string like effect: [ effect: { effect: " effect: n effect: a effect: m effect: e effect: " how can i solve this?
  • can you please update your question to figure out what's going wrong?