I have a local storage item as per below
(2) [{…}, {…}]
0
:
cityName
:
"durban"
__proto__
:
Object
1
:
cityName
:
"cape town"
__proto__
:
Object
I would like to delete 1 item only, based on whether the cityName matches the local storage item i.e if the user clicks "durban" then delete durban.
I already am able to get the name from the click
deleteCity(event){
var target = event.currentTarget.id;
console.log(target);
var getLSCityName = localStorage.getItem('savedLocations');
var getLSCityNameArr = JSON.parse(getLSCityName);
for(var i = 0; i < getLSCityNameArr.length; i++){
if (getLSCityNameArr[i].cityName == target){
getLSCityNameArr.splice[i].cityName;
localStorage.setItem('savedLocations', JSON.stringify(getLSCityNameArr));
}
}
}
savedLocationsrepresents an array, then use array filter method to remove the desired item