The lodash _.remove array method and vanilla javaScript alternatives

The process of removing a few elements from an array can sometimes be a little troubling, or at least I remember that it was back when I was first starting out with javaScript. The trouble was mainly with looping over an array from a zero element index value upwards, each time an element is removed it of course changes the length of an array, which of course causes a problem when looping forward threw array index values that way. One way that I would resolve the problem is by looping threw the array backwards, and using an array prototype method like Array.splice to purge elements out. For the most part that seems to work okay, but here is a wide range of other ways to go about doing this sort of thing.

So when it comes to just using native javaScript alone of course there are ways of just going ahead and removing elements from an array. However this is a post on lodash where it is assumed that lodash is part of the stack of a project, so with that said there is also the _.remove array method to work with. It works by passing a method that is used to define the conditions that are to be used to remove one or more elements from an array, and on top of that will mutate the array in place just like that of the array splice method. However unlike the array splice method I can define a condition by which to remove all elements that meet the condition, rather than a start index and count of elements.

There are however other methods of interest, both in lodash and native javaScript though such as the filter method that might be a better option as it will do the same only not mutate the array in place. Still lots of developers are transitioning away from lodash, so I will also be looking at some vanilla js alternatives such as array splice, but also other native methods. Also there are some additional related topics that come to mind such as the question of removing elements at all to begin with or not when it comes to the idea of just having a fixed set of elements that are simply reused rather than created and purged, such is the case with an object pool.

Read More

Lodash _.find collection method examples with vanilla js alternatives.

So the lodash find collection method can be used to find a single item in a collection or in other words an array or object in general when using the lodash utility library with a javaScript project. There is also the native Array.find method these days, but that is just an array prototype method, and as such it can not just be used to find an object key in general with any object unless that object is an array or array like object.

So the _.find method in lodash might be a little more robust then the native Array.find method, and as such can come in handy when I want to have a kind of method that will work great with collections in general. Where the native array prototype method is a little more limited. Still when it comes to finding something in an array, or collection there is more than one way to go about doing it. There is just finding one item in an array, and then there is sorting an array and taking the first item, or first few items after the sort. So lets look at the lodash find method as well as some other options for finding things with lodash an native javaScript.

Read More

The chunk array method in lodash, and chunking an array in general

So I think I will write a few posts on lodash, and as such why not start with the _.chunk array method. The lodash chunk method is a method in lodash that can break a linear array into an array of arrays, or in other words a multidimensional array as it may often be called.

So how often do I get into a situation in which I need to break down a linear array into an array of arrays? Maybe not to often but often enough that it would be nice to have a method that is part of a toolkit of sorts that makes quick work of it. In this case the lodash chunk method in is just that and will work well at least with most typical use case examples inn which I would want such a method.

There is also the question of flattening an array of arrays into a single array, for that there is the lodash flatten method. However in native javaScript there is also now a native flat method in the array prototype itself to work with for that kind of task. With that said there is a thing or two to write about when it comes to chunking an array with just native javaScript by itself also. So in this post I will be going over some examples that make use of the lodash chunk method of course, but I will also be looking into some native javaScript examples that do the same thing when it comes to just getting things done with javaScript by itself.

Read More

What is wrong with javaScript Modulo?

When working with many javaScript projects the use of modulo comes up from time to time. Modulo is an Arithmetic Operator in core javaScript that helps to find out the remainder of a division operation between two numbers.

Most of the time the javaScript modulo operator will work as expected just fine, however it follow a certain convention that might not be the same as some people might expect coming from other programing environments. This might not always be such a bad thing, but it can be when it produces different results for the same operation with the same values when compared to other ways of preforming a modulo operation. With javaScript modulo it is possible that you might run into problems when it comes to using the operator with negative numbers, and this might prompted a need for some other way to go about getting the remainder of two numbers. This raises the question what is wrong with the javaScript Modulo operator? Well nothing actually, yet something, in any case maybe this post might help to clear up some of the confusion.

Read More