从数组中删除元素-ERECT JS

发布于 2025-01-24 17:28:02 字数 716 浏览 3 评论 0原文

我正在制作一张材料表,当我按删除按钮时,材料必须消失并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2025-01-31 17:28:02

尝试以下操作:

RemoveMaterial = (material_id) => {

  //...
  
  const list = [...this.state.material_list];
  list.splice(material,1);
  this.setState({
      material_list: list
   });
   
   //...
  
  };

Try this:

RemoveMaterial = (material_id) => {

  //...
  
  const list = [...this.state.material_list];
  list.splice(material,1);
  this.setState({
      material_list: list
   });
   
   //...
  
  };

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文