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);
}
}
Answer from Poul Kruijt on Stack OverflowYou 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);
}
}
I think the Angular 2 way of doing this is the filter method:
this.data = this.data.filter(item => item !== data_item);
where data_item is the item that should be deleted
Same way as you would in JavaScript.
delete myArray[key];
Note that this sets the element to undefined.
Better to use the Array.prototype.splice function:
const index = myArray.indexOf(key, 0);
if (index > -1) {
myArray.splice(index, 1);
}
let foo_object; // Itemitem(object here) to remove
this.foo_objects = this.foo_objects.filter(obj => return obj !== foo_object);
You could use Array#filter method:
food = {
id: 1,
name: 'Pizza',
price: 16
};
orders = [
{ food_id: 2, table_id: 5 },
{ food_id: 2, table_id: 5 },
{ food_id: 1, table_id: 5 },
{ food_id: 3, table_id: 5 },
{ food_id: 1, table_id: 5 }
];
removeFoodOrder(food: Food): void {
this.orders = this.orders.filter(({ food_id }) => food_id !== food.id);
}
Edit:
Since your array allows duplicate elements and you want to remove only the first match, you could use the Array#findIndex + Array#filter methods:
const foundIndex = this.orders.findIndex(({ food_id }) => food_id === food.id);
this.orders = this.orders.filter((_, index) => index !== foundIndex);
The first step for me is always to remove anything confusing like that ternary operator and your break stmt. Here's how I did it
let food = {
id: 1,
name: 'Pizza',
price: 16
}
let orders = [
{food_id: 2, table_id: 5},
{food_id: 2, table_id: 5},
{food_id: 1, table_id: 5},
{food_id: 3, table_id: 5},
{food_id: 1, table_id: 5}
]
for (let order of this.orders) {
if (food.id === order.food_id) {
this.orders.splice(this.orders.indexOf(order), 1);
break;
}
}
console.log(this.orders);
I would recommend against using Array#filter if you don't know 100% how to use it.
UPDATE I am not saying don't use the Array#filter method. I'm just saying that if your code isn't working, you should try to remove anything that could be causing your issue and try to go about it step by step using simple constructs (like a for loop and if stmt).
<li *ngFor="let languague of listOfLanguagues; let i = index">
<button type="submit" (click)="removeLanguague(languague, i)" >Remove</button>
removeLanguague(languague, index){
this.listOfLanguagues.splice(index, 1);
}
You have to use splice and not slice
this.listOfLanguagues.splice(this.listOfLanguagues.indexOf(languague), 1);
slice returns a section of an array, and splice removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements