I have an interface with some fields like that:
interface Item {
id: number;
name: string;
}
And I want to implement a method removeItemsWithName(items: Item[], name: string)
which removes all items from the given array, which have the given name:
const myItems: Item[] = [
{ id: 1, name: 'foo' },
{ id: 2, name: 'foo' },
{ id: 3, name: 'bar' }
];
removeItemsWithName(myItems, 'foo');
console.log(myItems);
The result should be:
[{id: 3, name: 'bar'}]
Is there a method array.removeIf()
(like Collection.removeIf()
in Java 8) available in typescript/javascript, so that my method could look like something similar to this:
function removeItemsWithName(items: Item[], name: string): void {
items.removeIf(i => i.name === name);
}
I already found the question Remove array element based on object property, but I need to modify the original array in place instead of creating a new one (like array.filter()
does). I also want to remove all items matching the condition, not just the first one.