-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?

Miomir Dancevic
  • 6,071
  • 12
  • 53
  • 102

2 Answers2

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)

Zer0
  • 1,341
  • 7
  • 21
0

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

removeItem(index) {
    this.arrayItems.splice(index, 1);
}
Yogendra Chauhan
  • 690
  • 3
  • 15