filter()
has a more functional approach, which has its benefits. Working with immutable data is much more easier, concurrency and error safe.
But in your example, you are doing something similar by creating the items
array. So you are still not mutating any existing arrays.
const items = [...this.state.items];
Creates a copy of this.state.items
, thus it will not mutate them once you do a splice()
.
So considering you approach, it is no different than filter()
, so now it just boils down to a matter of taste.
const items = [...this.state.items];
items.splice(index, 1);
VS
this.state.items.filter(i => ...);
Also performance may be taken into consideration. Check this test for example.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…