我有一个对象数组。我需要添加一个函数来从数组中删除一个对象,而不使用“ this”关键字。
我试过使用 updateList(list.slice(list.indexOf(e.target.name, 1)))
。它删除了数组中除了最后一项之外的所有内容,我不知道为什么。
const defaultList = [
{ name: "ItemOne" },
{ name: "ItemTwo" },
{ name: "ItemThree" }]
const [list, updateList] = useState(defaultList);
const handleRemoveItem = e => {
updateList(list.slice(list.indexOf(e.target.name, 1)))
}
return (
{list.map(item => {
return (
<>
<span onClick={handleRemoveItem}>x </span>
<span>{item.name}</span>
</>
)}
}
)
预期: 单击的项目将从列表中删除。
实际: 整个列表被删除,减去数组中的最后一项。
提前感谢您的任何投入!