-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.
  • 35,047
  • 10
  • 74
  • 84

4 Answers4

0
this.editListModel = this.editListModel.slice(index, 1);
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
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
  • 376,160
  • 25
  • 347
  • 392
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,376
  • 8
  • 17
-1

You can remove item by using filter method

this.editListModel = this.editListModel.filter(item => item.id !== find.id);
Ikhtiyor
  • 819
  • 6
  • 8