localstorage array of objects code example
Example 1: set array of objects in localstorage
localStorage.setItem("savedData", JSON.stringify(objects));
objects = JSON.parse(localStorage.getItem("savedData")));
Example 2: array of objects javascript
var widgetTemplats = [
{
name: 'compass',
LocX: 35,
LocY: 312
},
{
name: 'another',
LocX: 52,
LocY: 32
}
]
Example 3: typescript array of objects
interface Product {
name: string;
price: number;
description: string;
}
let pen: Product = {
name: "Pen",
price: 1.43,
description: "Userful for writing"
}
let products: Product[] = [];
products.push(pen);
console.log(products);
Example 4: how push objects into a local stotage array
var existing = localStorage.getItem('myFavoriteSandwich');
existing = existing ? existing.split(',') : [];
existing.push('tuna');
localStorage.setItem('myFavoriteSandwich', existing.toString());