3

I have an object that I want to assign to the local storage , I mean can you loop over it ? any idea how to implement this ? The end result is the keys in the object must be keys as well in the local storage after using the setitem, I sure know how set and get singular items but in other words I want to mount and object on an empty local storage object.

 const data = JSON.parse(object); // a prev localstroage object 
 console.log(data);
 localStorage.setItem(...Object.keys(data), ...Object.values(data));
Richardson
  • 1,804
  • 10
  • 38
  • 1
    Does this answer your question? [Storing Objects in HTML5 localStorage](https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) – coagmano Nov 29 '21 at 05:22

3 Answers3

6

You have to use JSON.stringify(data); in order to store object in local storage.

Make sure you create your complete object in JavaScript itself (as per your requirement). And then once your object is ready, use JSON.stringify method to save objects in your local storage.

And to retreive back the data you will use JSON.parse() to get back the Object and then you can use any JavaScript methods to perform your tasks as per the requirement.

Imran Rafiq Rather
  • 7,677
  • 1
  • 16
  • 35
  • Sure , but what about the keys I mean my object has at least 10 keys !! How to push them at once ! – Richardson Nov 29 '21 at 06:24
  • Could you please add the syntax then in to your answer .. the syntax of setting the object with keys I want my object keys to by keys as well in the local storage rather than values…the model of ls should look exactly like the incoming object. – Richardson Nov 29 '21 at 06:28
  • 1
    Dear Friend @Richardson I am not exactly sure about what your requirement it Buddy. Can you give me more context. So that I can help you out :) – Imran Rafiq Rather Nov 29 '21 at 07:06
  • You need to do something like this my friend https://stackoverflow.com/questions/46340004/localstorage-get-every-value-from-each-key – Imran Rafiq Rather Nov 29 '21 at 07:31
  • lets say the local storages is empty, and my obj that i want to push to ls is the following , {Numbers : [1,2,3]}, i am not talking about parsing but about how to populate key values in local storage according to my obj that i want to push I want the local storage to look exactly like this Keys must be numbers and values must be [1,2,3] but how can achieve this in one single action ? – Richardson Nov 29 '21 at 23:52
0

 //To set
 const obj = {
   key: "Hello World"
 }
 const strObj = JSON.stringify(obj);
 localStorage.setItem('strObj', strObj);

 //To get
 const strObjFromStorage = localStorage.getItem('strObj')
 const objFromStorage = JSON.parse(strObjFromStorage)
 console.log(objFromStorage);
zakhefron
  • 1,403
  • 1
  • 9
  • 13
0

You should be able to do so with JSON.stringify and JSON.parse :

// save object to localStorage
const myobj = {
    x: 1,
    y: 6
};
localStorage.setItem('key', JSON.stringify(myobj));
// fetch date as object from localStorage
let data = JSON.parse(localStorate.getItem('key'));
outlandish
  • 82
  • 5
  • How would the final object look like in local storage ! I want my object to take over local storage in your answer your are stating key then the key with have the object of x and y, I want the x y to be at top level keys in the keys column rather than in value !! – Richardson Nov 29 '21 at 06:26