1//storing array in localStorage
2var colors = ["red","blue","green"];
3localStorage.setItem("my_colors", JSON.stringify(colors)); //store colors
4var storedColors = JSON.parse(localStorage.getItem("my_colors")); //get them back
1// Guardar el array en el localStorage
2
3// El arreglo:
4var array = [1, 2, 3];
5// Se guarda en localStorage despues de JSON stringificarlo
6localStorage.setItem('myArray', JSON.stringify(array));
7
8// Obtener el arreglo de localStorage
9
10var array = localStorage.getItem('myArray');
11// Se parsea para poder ser usado en js con JSON.parse :)
12array = JSON.parse(array);
1var myArray = [1,"word",23,-12,"string"];//your list of values, they can be strings or numbers
2
3//SAVING
4function save(a,b,c,d,e) {//the number of parameters you add is how many items you save
5 var itemArray = [a,b,c,d,e];//number of items here must be the same as number of parameters
6 var keyArray = ["one","two","three","four","five"];//must be same length as itemArray
7 for (var i = 0; i < keyArray.length; i++) {
8 if (typeof(itemArray[i]) === "number") {
9 itemArray[i] = "+"+itemArray[i];//leave a reminder that it was a number
10 }
11 if (typeof(itemArray[i]) !== undefined) {
12 localStorage.setItem(keyArray[i],itemArray[i]);//only add a value to the current key if it's not undefined
13 }
14 }
15}
16//to use the function (whenever you want to save):
17save.apply(this, myArray);//assuming that myArray is the array you want to set
18
19//LOADING
20function load() {
21 var list = [];
22 var keyArray = ["one","two","three","four","five"];
23 for (var i = 0; i < keyArray.length; i++) {
24 var item = localStorage.getItem(keyArray[i]);
25 if (item.charAt(0) == "+") {
26 item = parseFloat(item.substring(1));
27 }
28 list.push(item);
29 }
30 return list;
31}
32//to use the function
33myArray = load();//sets your array back to how it was when you saved it