//storing array in localStoragevar colors = ["red","blue","green"];
localStorage.setItem("my_colors", JSON.stringify(colors)); //store colorsvar storedColors =JSON.parse(localStorage.getItem("my_colors")); //get them back
Posted by: Guest
on July-31-2019
4
js save and load an array of different types into localstorage
var myArray = [1,"word",23,-12,"string"];//your list of values, they can be strings or numbers//SAVINGfunctionsave(a,b,c,d,e) {//the number of parameters you add is how many items you savevar itemArray = [a,b,c,d,e];//number of items here must be the same as number of parametersvar keyArray = ["one","two","three","four","five"];//must be same length as itemArrayfor (var i =0; i < keyArray.length; i++) {
if (typeof(itemArray[i]) ==="number") {
itemArray[i] ="+"+itemArray[i];//leave a reminder that it was a number
}
if (typeof(itemArray[i]) !== undefined) {
localStorage.setItem(keyArray[i],itemArray[i]);//only add a value to the current key if it's not undefined
}
}
}
//to use the function (whenever you want to save):
save.apply(this, myArray);//assuming that myArray is the array you want to set//LOADINGfunctionload() {
var list = [];
var keyArray = ["one","two","three","four","five"];
for (var i =0; i < keyArray.length; i++) {
var item = localStorage.getItem(keyArray[i]);
if (item.charAt(0) =="+") {
item =parseFloat(item.substring(1));
}
list.push(item);
}
return list;
}
//to use the functionmyArray = load();//sets your array back to how it was when you saved it
Posted by: Guest
on April-02-2020
Code answers related to "best module for localstorage stackoverflow"
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email