首页 > 解决方案 > 无法使用 delete 删除数组索引/项目

问题描述

我正在使用delete以尝试删除数组上的项目;

const handleRowDeletion = index => {
    const inputsAdded = shareStructureInputs;
    const inputs = startupFourthStepForm.shares_estructure;

    delete inputs[index];
    delete inputsAdded[index];
}

我可以看到第一个数组inputsAdded在正确的索引处被删除,但不是第二个inputs

这是inputsAdded返回:

[
  "share-structure-input-0",
  "share-structure-input-1",
  "share-structure-input-2"
]

并且inputs

[
  {
    "name": "Name 1",
    "percentage": 10
  },
  {
    "name": "Name 2",
    "percentage": 10
  },
  {
    "name": "Name 3",
    "percentage": 80
  }
]

我把这个函数称为map

                <Button onClick={() => handleRowDeletion(index)}>
                  - Delete Row
                </Button>

标签: javascriptecmascript-6

解决方案


推荐阅读