I have a service where there is a cards
array, which I looped thru in list-cards.html
component template with *ngFor="let card of this.cardService.cards
. This cards
array has a list of object created from API call, and it's rendered at app initialization as home page.
How do I filter that cards
array from the card.service.ts
in non-mutating way?
For now, it's like this (mutating way):
card.service.ts file:
this.cards = this.cards.filter((e: any) => e.id !== id);
If I create a new array variable, to filter it in non-mutating way, like in this example from: https://lorenstewart.me/2017/01/22/javascript-array-methods-mutating-vs-non-mutating/
const arr1 = ['a', 'b', 'c', 'd', 'e'];
const arr2 = arr1.filter(a => a !== 'e'); // ['a', 'b', 'c', 'd']
then it can't be looped any more with above *ngFor
declaration, because it's another array now, right!?
So, I came up to solution based on above example, where I've declared two array variables in the card.service.ts
file:
cards: any[];
// new array to avoid array mutation
newCards: any[] = [];
In the list-cards.ts
component in ngOnInit
directive I've declared:
// to render initial data from the array
this.cardService.newCards = this.cardService.cards;
and at list-cards.html
now I declared *ngFor="let card of this.cardService.newCards
,
and at last, filter method in card.service.ts
looks as follow:
this.newCards = [...this.cards].filter((e: any) => e.id !== id);
I am not really sure if my code accomplish the goal of non-mutating array??
I just find out that on delete action click, only once item is filtered. Every other delete click action is not filtered, but cards are deleted from db.
If I loop thru cards
array, not newCards
array with *ngFor="let card of this.cardService.cards"
everything work just fine, but then array is mutated!?