For Each in lodash and the native Array forEach
For one thing the lodash for each method is one of the many collection methods of lodash, which means that the method will work with collections in general, not just arrays. Also there are a few more little differences such as what happens when something is returned in the body of a function that is given to lodash for each which can be used as a way to stop looping early.
1 - The basics of lodash forEach
1.1 - basic example of lodash for each method
For a basic example of the lodash for each method in this example I am just passing an array of strings and then a function that will just log all the arguments for each element. The first argument in the function that I give to for each is the current value of a current element, then there is the index value and sense this is an array that I passed each value of index is a numbered index value. The third argument for the function given ti the lodash for each method is then a reference to the source object that I gave when calling for each, in this case the array of strings.
One of the main reasons why I choose to use a while loop over the native Array forEach method is to have the ability to use the break, and continue keywords to cause looping to come to and end. However with the for each method in lodash, I can return false in the body of the function I pass to it to break also. However that is not the only thing about lodash for each that is a little more than what array for each is, but it self at least.
1.2 - Lodash for each is a collection method
As I have mentioned the lodash for each method is a collection method, what this means is that it will work with objects in general, nit just arrays.
2.1 - Basic native array for each method example
So then of course there is the native array for each method. This native array method will work fine in most typically use case examples where I just want to simply loop over an array, but do not need to do anything like filtering, or reducing an array into a single primitive value as there are other array prototype methods that are a better choice.
So then with the native array for each method using return will have no effect, and also it is an array method so it will not work with just any object. Well that is that it will not just work with any object by itself at least, also there are other ways of looping in which I can use return, or break keywords. So maybe at least a few more examples are called for here.
2.2 - Object keys
To get array for each to work with any object there is making use of the static Object method known as Object keys. This Object keys method will take an object as a first argument when calling it, and the return value of the method will then be a new array that contains the public key names of the object. So then because the return value is an array I can then use the array for each method off of the result of this. However the values will be key names not the values of the source object, however I can of course use these key names to get values from the source object in the body of the function that I give to array for each.
2.3 - While loop
Just like with Array.forEach it some times makes sense to just use a loop of some kind. This allows for full control with respect to use of defining conditions with break, and continue, as well as complex conditions for starting, continuing, stepping, and additional escape conditions. There is also the matter that in some cases a loop is faster, but most of the time it does not make much of a difference, and I often view it as a nano pick issue.
3 - Conclusion