I have an app that writes data to and reads data from the localStorage.
Basically I have an array of objects that I am changing and reading many times in the code, and all of these changes I want to keep in sync with the localStorage.
Now I could create a function that syncs it every time I need it in the code, but it just sounds a bit ugly and unoptimized to me. Like this
arrayOfObjects.push(newItem);
localStorage.setItem('objects', arrayOfObjects);
and then
arrayOfObjects = localStorage.getItem('objects');
length = arrayOfObjects.length
This would be really bad, because I have to read and write to it hundreds of times in the code, terrible solution.
Now I could also encapsulate it into a function so that all writes and reads and syncs got through it, but still, it will lack many functionalities because I will still need to deal with arrayOfObjects as an array type for multiple array operations like .push and .length.
Any ideas on how I could make this array sync with the localStorage with the fewest possible lines of code? Is there some way I could just write/read to an array type directly in localStorage?