l am using Ionic 4 to build app weather and I added property to save cities in local storage .
I want to remove item weather city from local storage.
I did like that , but it doesn’t remove item from list:
deleteItem(city){
localStorage.removeItem('cities')
}
My full code including deleteItem
// get weather data from provider
getWeather(city :string){
this.weather.city(city).then(data=>{
if (data) {
this.weatherList.push(JSON.parse(data.data))
console.log(data.data)
// save city with data
this.saveWeatherEntry(city, data);
// test the storage,
let entry = this.getWeatherEntry(city);
console.log(city);
console.log(entry);
} else {
}
});
}
// check if exist before in local storage
saveWeatherEntry(city, data) {
let items_json = localStorage.getItem('cities');
let items = [];
if(items_json) {
items = JSON.parse(items_json);
let city_index = -1;
for(let i = 0; i < items.length; i++) {
if(items[i].city == city) {
city_index = i;
break;
}
}
if(city_index != -1) {
items[city_index].data = data;
} else {
items.push({
city: city,
data: data
});
}
} else {
items.push({
city: city,
data: data
});
}
//save changes
localStorage.setItem('cities', JSON.stringify(items));
}
// check if exist before in local storage
getWeatherEntry(city) {
let items_json = localStorage.getItem('cities');
if(items_json) {
let items = JSON.parse(items_json);
let city_index = -1;
for(let i = 0; i < items.length; i++) {
if(items[i].city == city) {
return items[i].data;
}
}
}
return false;
}
// delete item
deleteItem(city){
localStorage.removeItem('cities')
}
Any solution please?