从数组中删除元素-ERECT JS
我正在制作一张材料表,当我按删除按钮时,材料必须消失并使用 splice()方法,但是当我按下第一个元素的删除按钮时,此操作将删除全部数组的元素。
这是显然可以实现但行不通的功能:(
RemoveMaterial = (material_id) => {
let element = [...this.state.material_list];
const material = element.findIndex((item) => item === material_id);
const list = [...this.state.material_list];
const consumption_list = [...this.state.material_consumption];
const material_list = [...this.state.materials];
list.splice(material)
consumption_list.splice(material)
material_list.splice(material)
this.setState({
material_list: list,
material_consumption: consumption_list,
materials: material_list,
});
};
Im making a table of materials that, when i press the delete button a material have to disappear and im using the splice() method but when i push the delete button of the first element, this action deletes all the elements of the array.
Here is the function that apparently make this happen but isn't working :(
RemoveMaterial = (material_id) => {
let element = [...this.state.material_list];
const material = element.findIndex((item) => item === material_id);
const list = [...this.state.material_list];
const consumption_list = [...this.state.material_consumption];
const material_list = [...this.state.materials];
list.splice(material)
consumption_list.splice(material)
material_list.splice(material)
this.setState({
material_list: list,
material_consumption: consumption_list,
materials: material_list,
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
Try this: