The lodash pull method and other ways to remove elements
The lodash pull method can be used to remove one or more values from an array using the same value zero method as a way to make comparisons. This method is a kind of convenience method in place of using lodash remove with the lodash eq methods for example which would have the same end result. There are a number of other lodash methods such as filter and reduce that can also be used to preform similar tasks without mutating an array in place and will allow a little more flexibility in how equality is detected when it comes to using an operator or method other then that of the Same Value Zero compliant lodash eq method.
1 - lodash pull basics
1.1 - Basic lodash pull example
The basic idea of the lodash pull method is that I call it, pass and array as the first argument, and then one or more additional arguments that are values to remove from the array using the same value zero method that can be used by itself via the lodash eq method.
1.2 - The lodash pull methid will mutate an array in place
One draw back for many with the lodash pull method might be that the method will mutate an array in place.
1.3 - Objects
When it comes to an array of object the lodash pull method will only remove objects that are references to the same object given in memory. So then this is yet another reason why one might want to use some other method like that of filter, or reduce as a way to remove by some kind of given condition where a comparison is made with a primitive value of an object property rather than the object itself.
2 - Other lodash methods to be aware of
On top of the lodash pull method there is a number of other methods in lodash that work almost the same, but with slightly different ways that in some cases can be very significant. As I have covered in the basic section of this post the lodash pull method will mutate an array in place. In some cases I might not want to do that, but instead create a new array with elements that are not wanted not included in this new array. One way to do so the same way as that of the lodah pull method would be to use the lodash without method.
2.1 - lodash without and not muttaing in place
2.2 - The lodash eq method
The lodash eq method is how to go about doing a Same Value Zero comparison with two values that will return a boolean value rather than preform some kind of action such as using this as a way to remove all elements in place that will test true for this. In native javaScriot there is also the Object is method that will use a slightly different standard that is also like that of lodash eq, but it will treat comparisons between negative and positive zero differently.
2.3 - Using lodash remove, and lodash eq to do the same thing and more.
The lodash remove method is another way to go about removing methods from an array with lodash, only that will give a greater deal of flexibility as I can define the logic that will be used. I could use the lodash remove combined with the lodash eq method to do the same thing as lodash pull, or I could use a completely different expression all together in the function that I pass to the lodash remove method.
3 - A vanilla js pull to do lodash pull style pulling complete with an Object.is pony fill
Here I just copied over an Object is method and turned it into a pony fill called eq that will work the same way as the lodash eq method. Now that I have that I can use it on my vanilla js pull method. I am using the array filter method but could easily use array splice or slice to make it even more robust.
There are many other ways a vanilla js pull method could be written. If you do not care about supporting older browsers at all you could just use the Object is method, and make a far more concise solution at the cost of code breaking on older platforms.
4 - Conclusion
So then the lodash pull method will remove elements by a given value using the same value zero standard for doing so, and will do so by mutating the array in place. The lodash without method will do more or less the same thing as the lodash pull method but it will return a new array rather than mutating an array in place. There are then a number of other lodash method that can be used to preform the same task in a slightly more complex way, but may offer greater flexibility when it comes to making choices as to how elements should be removed from the resulting new array, or how an array should be mutated in place.
If you enjoyed this post and would like to read more on lodash there is my main post on lodash, as well as a lot of other posts that I have wrote on the topic of lodash.