Small Request for Lodash Library: Concatenating JavaScript Array In-place

Posted by & filed under , .

logo-lodashI started using Lodash a few months ago — it does make some of the mundane JavaScript tasks rather easier to perform, such as processing and traversing arrays and collections, filtering and so on. There is however one thing that I would like them to add, and it is to do with arrays concatenation.

(And in fact it’s not just Lodash but other javascript libraries that have the same issue.)

You might at this point throw at me the concat() method in Lodash however, if you look at the docco it reveals the following:

Creates a new array concatenating array with any additional arrays and/or values.

And that’s where my issue arises: “creates a new array“! Is there a way to append to the first array parameter? While when dealing with JavaScript, I do like to take a functional approach and deal with immutable data structures — in which case, the above approach is ideal — I do find ever so often that using references to arrays does help. In such cases, I would like to be able to change an existing array — and not create a new one.

So it would be nice to have _.pushAll(destArray, srcArray). All this would do is :

Array.prototype.concat.apply( destArray, srcArray );

and as such modify the destination array and not create a new one. Simple, no?

Leave a Reply

Your email address will not be published.