I am learning React and in one of the answer, I have seen the splice method is being used while deleting an item from the array. Link to the solution : Delete item from state array in react
To remove an element from an array, just do:
array.splice(index, 1); In your case:
removePeople(e) {
var array = [...this.state.people]; // make a separate copy of the array
var index = array.indexOf(e.target.value)
if (index !== -1) {
array.splice(index, 1);
this.setState({people: array});
}
},
Expected: As in many other blogs, we have used the filter method to delete the items from the list. why the answer is getting upvoted and we are not using filter method here ?