how to set and remove object from localstorage

This Article gives some basic knowledge about how set and get object from Browser LocalStorage.
Definition and Usage

The localStorage and sessionStorage properties allow to save key/value pairs in a web browser.

The localStorage object stores data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

The localStorage property is read-only.

Note: You need to use this code in javascript file

Set Json object in localStorage like this:

cData = []

var pHash = {'id': 123, 'name': 'xyz', 'type': 'x1'};

cData.push(pHash);

localStorage.setItem('object_name', JSON.stringify(cData));

Remove specific object from localStorage like this:

Sample Json Object:

object_name = [{"id":123,"name":"xyz","type":"x1"},{"id": 001,"name":"qrt","type":"x2"}]

for (var i = 0; i < JSON.parse(localStorage.getItem('object_name')).length; i++) {

 if (JSON.parse(localStorage.getItem('object_name'))[i]['id'] === 123) {

 ca = JSON.parse(localStorage.getItem('object_name'));

 ca.splice(i, 1)

 localStorage.setItem('object_name', JSON.stringify(ca));

}