For those looking to efficiently remove items from an array, Underscore offers the _without() method as a perfect solution.
This method returns a modified copy of the original array with specified values removed.
_.without(["apple", "banana", "cherry"], "apple");
=> ["banana", "cherry"]
The flexibility of _without() extends to more complex objects as demonstrated below.
var apple = { Name: "Apple", Price: 1.99 };
var banana = { Name: "Banana", Price: 0.79 };
var cherry = { Name: "Cherry", Price: 3.49 };
var fruits = [apple, banana, cherry]
_.without(fruits, banana);
=> [{ Name: "Apple", Price: 1.99 }, { Name: "Cherry", Price: 3.49 }];
If you only have a property value to identify the item for removal, combining _.findWhere
and _.without
is another effective approach.