how to set array in localstorage code example

Example 1: javascript store array in localstorage

//storing array in localStorage
var colors = ["red","blue","green"];
localStorage.setItem("my_colors", JSON.stringify(colors)); //store colors
var storedColors = JSON.parse(localStorage.getItem("my_colors")); //get them back

Example 2: localstorage save array

var names = [];
names[0] = prompt("New member name?");
localStorage.setItem("names", JSON.stringify(names));

//...
var storedNames = JSON.parse(localStorage.getItem("names"));

Example 3: set array of objects in localstorage

//set item in local storage
localStorage.setItem("savedData", JSON.stringify(objects));

//retrieve item from local storage
objects = JSON.parse(localStorage.getItem("savedData")));