-1

I know i can remove last item from array and first but I need to remove specified item based value Here is my array

 arrayItems: { volume: number; title: string; }[]; this.arrayItems = [{ volume: 11, title: 'Title' }]; 

Here is my example i have html

 <div *ngFor="let arrayItem of arrayItems; let i=index"> <label>{{arrayItem.i}}</label> <div (click)="removeItem(i)">remove item</div> </div> 

And to add and remove item

 addItem() { const item = { volume: 2, title: 'Naslov 2' }; this.arrayItems.push(item); } removeItem(index) { this.arrayItems.slice(index, 1); } 

But to removeItem() does not work, any idea why this is wrong?

1
  • 1
    Use splice and not slice. Commented Aug 27, 2020 at 6:28

2 Answers 2

1

This is because you use Array.slice, use Array.splice instead. SLice doesn't change the original array but returns a new one, splice however also modifies the original array (for reference, see here)

Sign up to request clarification or add additional context in comments.

Comments

0

Use splice to remove specified item from the JSON array like below:

removeItem(index) { this.arrayItems.splice(index, 1); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.