Chains and the lodash thru method
This will be a post on the lodash thru method that is one of several useful methods when working with a chain in lodash. The other useful method to take into account would be the lodash tap method that I wrote a post on earlier this month as I take a moment to expand on lodash, and edit some older posts on the topic too while I am at it. There is also the question of how to even go about starting a chain in lodash to begin with, when it comes to that there is the main lodash function, as well as the lodash chain method.
1 - Basics of the lodash thru method and also chaining in lodash
1.1 - Just calling methods and passing arguments
If you have not yet figured out chaining in lodash you might find yourself calling a lodash method as an argument for another lodash method and so on. Although this might work, there are lodash built in ways to avoid having to do this sort of thing.
1.2 - Using lodash chain
One way to avoid having to call lodash methods as arguments for additional lodash methods would be to make use of the lodash chain method.
In some situations I might end up making a long chain of lodash methods and at some point in that chain it would be nice to have a way to tap into the chain and just do something custom with the value then and there. This is where methods like the lodash thru method will come into play.
1.3 - Using lodash thru
When making a chain the lodash thru method can be called at any point in the chain, and a function can be passed as the first and only argument for this thru method. Inside the body of the function that is passed to thru the return keyword will be used to return what will be the new value for the chain at that point.
2 - Other lodash methods
There are a number of other lodash methods that come to mind when it comes to the subject of chaining in lodash. The thru method is great for not just tapping into a chain but also to have a great deal of control when it comes to redefining what the value of the chain should be at a given point. If for some reason I just want to simply tab into a chain at some point to just log out what the result is at a given point, or mutate an object in place rather than returning something they there are other methods to make use of such as tap.
2.1 - The lodash tap method
The tap method in lodash is what I would want to use to just tap into a chain and do something like just logging what the value is at a given point.
The tap method can also be used to mutate the value also, but only by preforming some kind of action that is mutation in place of an object value. This is because the return keyword is not used at all when lodash tap is used in place of lodash thru.
2.2 - The lodash mixin method
Would you say that lodash is missing something? In other words do you find yourself is a situation in which there is one or more methods that come to mind that should be a part of lodash yet it is not? Well then when it comes to customizing lodash with a few methods that might prove to be this kind of method they can be added in for a project by way of the lodash mixin method.
3 - Conclusion
That is it for now at least when it comes to the lodash thru method until I come around to do a little more editing on this post at some point in the future. That might be a while though as I can not say writing new lodash content is a high priority for me at this time, but I do still get around to doing so never the less.