I have an array of string and want to instantly remove some of them. But it doesn't work
var list = ['a', 'b', 'c', 'd']
_.remove(list, 'b');
console.log(list); // 'b' still there
I guess it happened because _.remove
function accept string as second argument and considers that is property name. How to make lodash do an equality check in this case?
remove() method in Lodash removes all the elements from an array that returns a truthy value for the specified predicate. This method will mutate the original array and return an array of all the removed elements.
remove() Method. The _. remove() method is used to remove all elements from the array that predicate returns True and returns the removed elements.
One more option for you is to use _.pull, which unlike _.without, does not create a copy of the array, but only modifies it instead:
_.pull(list, 'b'); // ['a', 'c', 'd']
Reference: https://lodash.com/docs#pull
As Giuseppe Pes points out, _.remove
is expecting a function. A more direct way to do what you want is to use _.without
instead, which does take elements to remove directly.
_.without(['a','b','c','d'], 'b'); //['a','c','d']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With