根据对象数组中的索引(typescript+nextjs)过滤掉对象



im试图通过使用其索引从列表中筛选出我想要删除的项。

示例:我想删除索引为0的项目,id将0传递给筛选函数,以在所有位置返回对象,而在位置0没有对象

这是我目前拥有的:

function removeCartItemHandler(index) {
console.log(index)
if (index != -1) {
cart = cart.splice(index, 1);
cart.filter(cart[index]);
setCartItems(cart);
}
console.log(cart)
}

我看到你使用了钩子,一般注意的是使用修饰符func,而不是操纵原始状态cart。类似的东西


function removeCartItemHandler(index) {
console.log(index)
if (index != -1) {
setCartItems(prevItems => prevItems.filter((element, elementIndex) => { return elementIndex !== index  } ));
}
console.log(cart)
}

最新更新