JSFiddle - React, Tailwind, and code Playground
JavaScript
function CheckPropDoup(array1,propName,propVal) {
for(var i=0,k=array1.length;i<k;i++){
if(array1[i][propName]==propVal) return true;
}
return false;
}
function saveStore(x,y,z) {
if(typeof(Storage)!=="undefined") {
var ds = JSON.parse(localStorage.getItem(x)) || [];
var obj = {};
obj[y] = z;
if(!CheckPropDoup(ds,y,obj[y])) ds.push(obj);
localStorage.setItem(x, JSON.stringify(ds));
}
}
function getStore(x) {
if(typeof(Storage)!=="undefined") {
return JSON.parse(localStorage.getItem(x)) || [];
}
}
function removeStore(x,y,z) {
if(typeof(Storage)!=="undefined") {
var ds = JSON.parse(localStorage.getItem(x)) || [];
for (i = 0; i < ds.length; i++)
if (ds[i].fruit === z) {
ds.splice(i, 1);
break;
}
localStorage.setItem(x, JSON.stringify(ds));
}
}
saveStore('food', 'fruit','banana');
saveStore('food', 'fruit','peach');
saveStore('food', 'fruit','apple');
alert( JSON.stringify(getStore('food')).replace(/,/g,',\n') );
removeStore('food', 'fruit','banana');
alert( JSON.stringify(getStore('food')).replace(/,/g,',\n') );