JavaScript切片无法正常工作

sha*_*e01 1 javascript arrays angularjs

我试图克隆我的数组,然后使用slice()函数从中删除一个元素.但是,每当我单击要删除的元素时,它会删除数组中除了单击之外的所有内容.

这是我目前的代码:

deleteContact(contacts: Contacts){
if (contacts === null || contacts === undefined) {
  return;
}

const pos = this.contacts.indexOf(contacts);
if (pos < 0) {
  return;
}

this.contacts = this.contacts.splice(pos, 1);
this.contactsListClone = this.contacts.slice();
this.contactListChangedEvent.next(this.contactsListClone);
}
Run Code Online (Sandbox Code Playgroud)

gur*_*372 8

splice返回已删除的元素,因此this.contacts此行后只有一个已删除的元素

this.contacts = this.contacts.splice(pos, 1);
Run Code Online (Sandbox Code Playgroud)

简单地做

this.contacts.splice(pos, 1);
Run Code Online (Sandbox Code Playgroud)