Akercode.com

JavaScript localStorage array of objects

JavaScript localStorage can save array of objects like any variable, the only thing you have to do is to stringify the objects before you save them.

Save array of object to localStorage:

const cars = [
  { type: "Fiat", model: "500", weight: "850kg" },
  { type: "Volvo", model: "240", weight: "1243kg" },
];

localStorage.setItem("savedData", JSON.stringify(cars));

Get saved data from localStorage as JSON:

const savedCars = localStorage.getItem("savedData");
console.log(savedCars);

Image of array of objects as JSON written out in console log in chrome web browser

Get saved data from localStorage as array of objects:

const savedCars = JSON.parse(localStorage.getItem("savedData"));
console.log(savedCars);

Image of array of objects written out in console log in chrome web browser