Using Array.pop和Array.indexOf在一起不起作用



我试图使用Array.pop(Array.indexOf(value)),但它删除了索引中的最后一个元素,即使该索引的值与我设置的值不一样,你能帮助我吗

CheckBoxHandler1(e) {
console.log(e.target.checked, e.target.name);
let temp = [...this.state.boxes];
e.target.checked === true
? temp.push(e.target.name)
: temp.pop(temp.indexOf(e.target.name));
this.setState({
boxes: temp,
});
}

这是因为Array.prototype.pop()总是删除数组的最后一个元素。

如果您想要删除一个特定的元素,您需要执行以下操作:

temp.splice(temp.indexOf(e.target.name), 1);

相关内容

最新更新