I am trying to clone my array and then delete one element from it by using the slice() function. However, whenever I click on the element I want to be deleted, it deletes everything in the array except for the one that I click.
Here is my current code:
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);
}
splice returns deleted elements, so this.contacts has only one deleted elements after this line
this.contacts = this.contacts.splice(pos, 1);
simply make it
this.contacts.splice(pos, 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With