In this lodash post I will be writing about the lodash _.concat method, and of course the corresponding vanilla js method Array.concat. Regardless of which one you use the result is the same, adding two or more arrays into a single array in other words concatenation of arrays.
Still it is there just for the hell of it, and looking into the lodash source code, it looks like the lodash developers are not just directly referencing the native method, as is the case with some of these methods.
Although this is a lodash post on _.concat, this is also a kind of post of concatenating arrays in general. Also although there might not be much of a point using _.concat by itself in light of the vanilla js Array.concat, diving deep into the source code _.concat uses some internal methods that are shared with related methods like _.flattenDeep.
For a basic example of concat what is more basic then just some arrays of primitives right? In this section basic example of array concatenation will be covered using lodash _.concat, and Array.concat.
So the _.concat method works by just calling the method, and then giving the arrays, and elements to combine into an array.
This can be any mixture of arrays or values. primitives like always will be copy’s, however objects, will of course be references so be careful about that. However for the most part that is all there is to it, and this is often preferable to a more complex alternative that involves creating a new array, and looping.
The vanilla js method Array.concat works in a very similar fashion, the only note worth difference would appear to be that is a prototype method of Array rather that a stand alone method that is given arguments like with _.concat.