I have an Ionic App where I am storing the data in the localStorage. The requirement is that I want to delete some items from an array in an object on the localStorage based on user click.
I have the below code but somehow not working. Please suggest where I am doing the mistake.
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));
}
my localStorage looks like this:
Key : Data
Value : {"data":[[0,"1600074900000"],[1,"1600679760000"]]}
Issue is the above home.ts is deleting the complete array instead of item clicked.
The function (click)="deletereminder(item)" returns the correct item to be deleted. example if i click on any item 0,"1600074900000", it will return 0,"1600074900000". I have checked through console.log(item).
But it is not deleting the item i am selecting. Instead it is deleting items from top of the array. example, if array has 3 records, it is deleting records from top to bottom. And also when it reaches the last record, it is not deleting the last record.
itemand the array/object you are setting in local storage so that we can have better idea to play around.