Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript slice not working

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);
}
like image 922
shae01 Avatar asked Mar 11 '26 17:03

shae01


1 Answers

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);
like image 171
gurvinder372 Avatar answered Mar 14 '26 07:03

gurvinder372



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!