The lodash remove method is one of the many array methods in lodash that work with arrays, there are some additional options to be aware of such as the pull, without, and filter methods in lodash. The remove method is a method where a function must be passed to define what the condition is for removing and element from an array, and the remove method will also mutate the array in place. The other options in lodash do more or less the same as remove, only a value can be given rather than a function, and some of them will not mutate the source array.
So for starters in this section I will be writing about just a few quick simple examples using the lodash remove method, and I also often take a moment to mention some other things you might want to be aware of in this section before continuing to read the rest of this post.
It’s pretty easy, just pass the array, and a method where you can define the conditions that will result in removal of the element that you want out of there. This method that is passed as the second argument will be given each element as an argument, and this of course can be used to define an expression that will evaluate to true or false. The return keyword can then be used in the body of that method, and if a true value is returned then the element in question will be removed from the array in place.
So the method that you pass will return true of false, if what is returned is true the element will be removed.
There is always taking note of what a return value of for a function if any, even for methods that mutate in place like the remove method. With that said the remove method is not like other methods that return a new array, so there is no need to set the return value of a new array that is returned t a variable that may have contained the source array like with other methods. However there is still a return value for the lodash remove method it is just that the return value is an array of elements that where removed from the array.
I have the source code of the examples in this post up on my test lodash Github repository. In that folder I am also drafting out future edits for this post, and the repository also contains the source code examples for all my other posts on lodash. I do get around to editing my content now and then so if you see anything that might need to change, or something that should be added there is making a pull request there, or leaving a comment down below on this site.
So one little problem with the lodash remove method is that it will mutate the array in place. In some situations I might not want to mutate the given source array, so another lodash method that comes to mind when it comes to removing elements from an array is the lodash filter method. This method works more or less the same way as the lodash remove method only with a few differences. One as I mentioned is that it will return a new array, rather than mutating the source array, and the other is that a true boolean value is what is returned to find out what elements are to remain rather than be removed.
When it comes to making a game that involves an array of enemies that the player interacts with, often there will be some kind of process that involves purging enemies from an array. The\ _.remove method can be used to make quick work of that, mutating the array in place, and also making the code easy to read.
The array slice method will return a new array from a source array at a starting and ending index values in the source. So then this is a way to go about creating a new array rather than mutating an array in place like the lodash remove method does.
One draw back of the array slice method then is that by calling the method just once I can only get one element or a few elements that are next to each other. So when it comes to creating a new array of elements that meet or do not meet a given condition I am going to need to do a bit more than just use the array slice method alone.
So then there is not just using array slice by itself, but using it in conjunction with the array concat method as a way to furnish a new array with elements that I want, there by removing elements that I do not want.
When removing more than one element with Array.splice in a loop such as a while loop a problem may come up that has to do with the fact that the length of the array changing when one or more elements are removed.
So making a remove method with Array.splice is not to hard. If you are not familiar with how to write your own higher order functions then it is not a bad idea to make one or two now and then, even if they are kind of basic. A higher order function is just a fancy term that is used to refer to a function that accepts another function as an argument and or returns another function when called. This example is then an exercise of writing something that is the latter of the two, sense I will be returning an Array..
There are of course many different ways that a function such as this could be written. I like Array.splice because of the great browser support, but if you are not such a nut with that sort of thing another option might involve the use of Array.filter for example.
So lodash is packed full of helpful little methods like the lodash remove method for removing elements from an array, but there are a few other options that should also be taken into consideration. It is true that many of the methods in lodash are redundant, but that is not always the case. Sometimes a lodash method does bring a little more to the table compared to a native counterpart.
If you enjoyed reading this post you might want to check out my main post on lodash in general.