I want to remove an item from a stored array in angular 2, with Type Script. I am using a service called Data Service, the DataService Code:
export cl
You can't use delete
to remove an item from an array. This is only used to remove a property from an object.
You should use splice to remove an element from an array:
deleteMsg(msg:string) {
const index: number = this.data.indexOf(msg);
if (index !== -1) {
this.data.splice(index, 1);
}
}
//declaration
list: Array<any> = new Array<any>();
//remove item from an array
removeitem()
{
const index = this.list.findIndex(user => user._id === 2);
this.list.splice(index, 1);
}
That work for me
this.array.pop(index);
for example index = 3
this.array.pop(3);
<tbody *ngFor="let emp of $emps;let i=index">
<button (click)="deleteEmployee(i)">Delete</button></td>
and
deleteEmployee(i)
{
this.$emps.splice(i,1);
}
You can use like this:
removeDepartment(name: string): void {
this.departments = this.departments.filter(item => item != name);
}