I need to set an array of objects values to localstorage using vanilla JavaScript.
I have tried looping through the array and getting each index then setting each index it doesn't seem to work. However if i single reference each element it works, which is strange...
const theme_cache = [document.getElementById('panel').style.background,document.getElementsByClassName('icon-bar')[0].style.color]
const saved_theme_cache = JSON.parse(localStorage.getItem('cached-elements'));
element.onchange = function(){
localStorage.setItem('cached-elements', JSON.stringify(theme_cache));
}
toStringrepresentation. You should use JSON.stringify and JSON.parse to serialize/deserialize the valueJSON.stringify()to store the array as a JSON string andJSON.parse()to restore the array of objects from the stored string.