Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage getItem logs[object Object]

I am trying to set and get items from local storage but when i log the data i get [object Object]

i am trying to get the view of that object something like this {a : v, b : v }...

here is the code :

var widgets = localStorage.getItem('widgets');
        if (widgets == null) {
         widgets = {
            widget_lot : '',
            widget_td : '',
            widget_cwo : '',
            widget_vehicles : '',
            widget_take : ''
            };  

            widgets.widget_lot = 0;
            widgets.widget_td = 0;
            widgets.widget_cwo = 1;
            widgets.widget_vehicles = 0;
            widgets.widget_take = 0;

            localStorage.setItem('widgets', widgets);
        }

        console.log(widgets); //Logs "[object Object]"
like image 717
zinho Avatar asked May 22 '14 11:05

zinho


People also ask

What is the difference between localStorage getitem () and localStorage setitem ()?

localStorage.setItem ('widgets', JSON.stringify (widgets)); localStorage stores the value as string, so when you try to save an object to toString () method of the value to be stored is called, since you have a object it will return [object Object] In the same way, getItem () returns a string, to convert it to a object use JSON.parse ()

How to get the value of the specified local storage item?

Get the value of the specified local storage item: var x = localStorage.getItem("mytime"); Try it Yourself » Definition and Usage The getItem() method returns value of the specified Storage Object item. The getItem() method belongs to the Storage Object, which can be either a localStorageobject or a sessionStorageobject. Browser Support Method

Does localStorage support ToString () method?

Local storage only supports string datatype. So you have to Show activity on this post. localStorage stores the value as string, so when you try to save an object to toString () method of the value to be stored is called, since you have a object it will return [object Object]

How to delete all the items in localStorage?

Then to retrieve it from the store and convert to an object again: var user = JSON.parse (localStorage.getItem ('user')); If we need to delete all entries of the store we can simply do: localStorage.clear (); This is a 10-year-old question.


Video Answer


2 Answers

Local storage only supports string datatype. So you have to

  1. Convert it to String before saving to LocalStorage

    localStorage.setItem('key', JSON.stringify(data));
    
  2. Convert back to JS object, reading from LocalStorage

    data = JSON.parse(localStorage.getItem('key')); //forgot to close
    

In case of your code, it should be -

var widgets = JSON.parse(localStorage.getItem('widgets'));

and

localStorage.setItem('widgets', JSON.stringify(widgets));
like image 194
brainless coder Avatar answered Oct 22 '22 19:10

brainless coder


You need to stringify the object before storing it to the storage like

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

localStorage stores the value as string, so when you try to save an object to toString() method of the value to be stored is called, since you have a object it will return [object Object]

In the same way, getItem() returns a string, to convert it to a object use JSON.parse()

so

var widgets = localStorage.getItem('widgets');
console.log('stored', widgets)
if (widgets == null) {
    widgets = {
        widget_lot: '',
        widget_td: '',
        widget_cwo: '',
        widget_vehicles: '',
        widget_take: ''
    };

    widgets.widget_lot = 0;
    widgets.widget_td = 0;
    widgets.widget_cwo = 1;
    widgets.widget_vehicles = 0;
    widgets.widget_take = 0;

    localStorage.setItem('widgets', JSON.stringify(widgets));
} else {
    widgets = JSON.parse(widgets)
}

console.log(widgets); //Logs "[object Object]"

Demo: Fiddle

like image 20
Arun P Johny Avatar answered Oct 22 '22 19:10

Arun P Johny