The main idea about the _.compact method is that it can be used to quickly remove false values away from an array. With that said the compact method is one of the array rather than collection methods in lodash, so then the compact method is not a replacement for other lodash methods like that of the lodash filter method. Also there are other array method options in lodash such as the lodash remove method that allows for a little more flexibility when it comes to defining what should not be in the new array that is to be cerated. Covering the method by itself is not that involved, but it can branch off into some additional topics when it comes to doing the same with just plain old vanilla js.
The source code examples in this post can be found in my test lodash repository on Github. This test lodash repository is the current repository where I intend to have the source code examples for my many other posts on lodash as I go threw and edit my content on lodash.
For a basic example of this say we just have a simple array with some values in it. Some of these values will evaluate to false if you where to convert them to boolean, others will not. Say you want to just remove all the values that will evaluate to false, and keep the ones that do not. there are many ways to go about doing this, but if lodash is part of your projects stack the _.compact method can be used to make quick work of this. There are of course other methods in lodash, and doing so with plain old vanilla js is not big deal as well, so lets take a look at some examples.
So of course lets start of with using lodash _.compact method first, doing so is stupid simple just call the method passing the array that you want false values removed, and the desired array will be returned.
Another option in lodash for making an array more compact would be the lodash remove method. One draw back of this lodash remove method compared to the compact method is that the remove method will mutate an array in place. If this is a problem though such an issue can often easily be filed by using a method like lodash clone, or lodash clone deep when it comes to situations involving nested objects that also need to be cloned.
The main advantage that is gained from using remove over compact is that I can define a custom method to figure out what should and should not be removed. With that said I can use that as a way to remove what I want gone when it comes to mutating an array in place, but I can also create a clone of the source array, and invert the logic to get a return value with a desired outcome also.
It is not to hard to make a vanilla js solution using Array forEach, and drop the use of lodash for this kind of task. It is true that all we are doing here is that we are looping over the contents of an array, and applying a condition for each element, if that condition is true, the element is then pushed to a new array.
The lodash compact method will quickly create a new array that is a source array wit all the false elements removed. However there are a number of draw backs with this compared to other options like the remove method in lodash that is a bit more flexible compared to compact when it comes to defining some custom logic of what it is that should not be in a new array. There are other methods such as that of the lodash filter method that is an examples of a collection method in lodash that will work with arrays as well as objects in general.