I want to remove a specific element from an array, I am getting the key of the element from the input. I want to be able to remove the element only by knowing the key.
This is the array:
state ={
splitAmount : [{
"SplitAmount0": this.props.data.amount1
}, {
"SplitAmount1": this.props.data.amount2
}, {
"SplitAmount2": this.props.data.amount3
}]
}
Remove function:
removeSplitAmount(e) {
console.log("remove",e.target.name)
let array = [...this.state.splitAmount];
let index = this.state.splitAmount.IndexOf(p => p == e.target.name )
if (index !== -1) {
array.splice(index, 1);
this.setState({splitAmount: array});
}
}