One of my challenges involves an Ionic
App that stores data in the localStorage
. I need to remove specific items from an array within an object in the localStorage
when a user clicks on them.
Even though I have the code below, it doesn't seem to be functioning properly. Can anyone point out where I might be going wrong?
home.html:
<ng-container *ngFor="let item of items[0]">
<ion-item no-lines class="items" *ngIf='this.items!= 0' (click)="deletereminder(item)">
<ion-row>
<ion-col>
<h3 ion-text class="items">
Reminder {{item [0] + 1}}
</h3>
</ion-col>
<ion-col>
<h3 ion-text class="items">
{{item [1] | date:'medium'}}
</h3>
</ion-col>
<ion-col style="text-align: right;">
<h3 ion-text class="itemdelete">
<ion-icon name="trash"></ion-icon>
</h3>
</ion-col>
</ion-row>
</ion-item>
</ng-container>
home.ts:
deletereminder(item){
var newObj = JSON.parse(localStorage.getItem('Data'));
console.log("index to delete",item);
newObj.data.splice(item, 1)
localStorage.setItem('Data', JSON.stringify(newObj));
}
This is what my localStorage
looks like:
Key : Data
Value : {"data":[[0,"1600074900000"],[1,"1600679760000"]]}
The issue with the above home.ts
file is that instead of deleting the clicked item, it's removing the entire array
. The function
(click)="deletereminder(item)"
does provide the correct item to be deleted when tested with console.log(item)
, but it strangely deletes items starting from the top of the array downwards. Moreover, it fails to delete the last record when reached.