-3

I have this list :

0: {id: 1, scoreValue: 33, coinValue: 1}
1: {id: 2, coinValue: 41, scoreValue: 1}
2: {id: 3, scoreValue: 33, coinValue: 0}
3: {id: 3, coinValue: 41, scoreValue: 33}
4: {id: 3, coinValue: 44, scoreValue: 33}

and I need to remove the item from this list with this code:

        let model = {} as EdiltListModel;
        let find = this.editListModel.find(x => x.id === id);

        if (find !== undefined) {
            model.id = find.id;
            model.scoreValue = parseInt($event.target.value);
            model.coinValue = coinValue;
            const index = this.editListModel.findIndex(x => x.id = find.id);
            this.editListModel = this.editListModel.slice(index, 1);
        } else {
            model.id = id;
            model.scoreValue = parseInt($event.target.value);
            model.coinValue = parseInt(coinValue);
        }
        this.editListModel.push(model);

but it not remove that item. How can I remove item from this list?

Aleksey L.
  • 25,602
  • 7
  • 55
  • 64

4 Answers4

0
this.editListModel = this.editListModel.slice(index, 1);
MERN
  • 2,842
  • 6
  • 13
  • 34
0

You need to take Array#splice. This removes the items.

this.editListModel.splice(index, 1);

Array#slice takes a copy from the array.

Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

The deleting logic can be done with filter You will archive that as follows.

this.editListModel = this.editListModel.filter(model => model.id != id)
harisu
  • 1,192
  • 5
  • 15
-1

You can remove item by using filter method

this.editListModel = this.editListModel.filter(item => item.id !== find.id);
Ikhtiyor
  • 781
  • 5
  • 7