I am trying to add and delete an obect from an array, I have been able to figure out the add object part, but the deletion is not working.(我试图添加和删除数组中的对象,我已经能够找出添加对象部分,但是删除不起作用。)
I have used filter() but it did nothing.(我已经使用filter(),但是它什么也没做。) Now I am using splice, it works but it deletes the first element, instead of the selected item.(现在我正在使用拼接,它可以工作,但是它删除第一个元素,而不是选定的项目。) below is a sample code, and I have shown only the functions for better clarity.(下面是一个示例代码,为了更好地说明,我仅显示了这些功能。)
handleDelete(item) {
this.setState(({ list}) => {
const newList = [...list];
newList.splice(item.key, 1);
console.log('deleted', newList);
return { list: newList };
});
}
handleAdd() {
const { firstname, lastname, email, phone} = this.state;
const ID = uuid();
const newItemObject = {
key: ID,
firstname: firstname,
lastname: lastname,
email: email,
phone: phone,
image: null,
};
this.setState(prevState => ({
list: [...prevState.list, newItemObject]
}));
}
I would like to(我想要)
ask by vincent O translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…