Jayden Webb
Jayden Webb

Reputation: 39

Can a localStorage value be an object or array?

I am trying to set storage in the user's computer with localStorage("save","{teams:5, points:20}, etc. But I'm not sure if the value can be an object. I've checked the internet, but I can't seem to find clear answers there.

var data={
teams:5, points:25,team1:5,team2:10, team3:5, team4:0, team5:5};
localStorage ("save", data);

Thoughts?

Upvotes: 2

Views: 148

Answers (5)

pushpenduP
pushpenduP

Reputation: 71

No, localstorage takes string as and saves it. If you want to save any object then convert it on string and then save it. while fetching it from local storage you we get string. you may easily covert it into object. if you directly save an object into localStorage then it will be saved in such manner "[object Object]" and while fetching it from localStorage, you will get "[object Object]" as String.

Wrong Code:

var data={teams:5, points:25,team1:5,team2:10, team3:5, team4:0, team5:5};
localStorage.setItem("save", data);
localStorage.getItem("save")

you will get "[object Object]"

Right Code:

var data={teams:5, points:25,team1:5,team2:10, team3:5, team4:0, team5:5};
localStorage.setItem("save",JSON.stringify(data));
localStorage.getItem("save")

While fetching value from localStorage

Wrong Code:

localStorage.getItem("save")

It will fetch data in form of string.

Right Code:

JSON.parse(localStorage.getItem("save"))

It will return data in form of object.

I hope the solution is well explained.

Upvotes: 1

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You need to use the localStorage.setItem(keyName, keyValue);

For Storing a Object, you can make use of JSON.stringify(object) which converts the object to a string.

From MDN

keyName
A DOMString containing the name of the key you want to create/update.

keyValue
A DOMString containing the value you want to give the key you are creating/updating.

Upvotes: 0

Erhan Kılıç
Erhan Kılıç

Reputation: 21

No, It can't be but you can use stringify. For example:

localStorage.setItem('user', JSON.stringify(r.user));

Upvotes: 0

Curiousdev
Curiousdev

Reputation: 5778

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));

Upvotes: 2

Phil
Phil

Reputation: 164744

No, the key and value must be a DOMString. See https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem

What you can do is serialise your data as JSON before setting it

localStorage.setItem('save', JSON.stringify(data))

When reading the value, you can parse it back into an object

let saveData = JSON.parse(localStorage.getItem('save'))

Upvotes: 4

Related Questions