So the lodash invoke map method is used by calling the method and then passing the collection as the first argument. The second argument is then a path to a method in the given collection object, or a function to use in place for such a method that is to be called for all elements in the collection. Any additional arguments are then arguments that are to be passed to the method that is to be called.
When calling invoke map the first argument should be the collection that I want to use invoke map with, such as an array fo numbers for example. The second argument is then a path to the function to call for each element, or the function itself to use. Then after that it is a question of what values to use for the arguments when calling the given function that is what the additional arguments are for at that point.
For this example I am using the array sort method for each nested array in an array of arrays.
There is also the lodash map collection method also that will work in a very similar way to that of invoke map, but will give a grater amount of control when it comes to what to do for each element in a given collection. However there are still ways to go about having the same amount of control actually when it comes to using invoke map it is just that things need to be done a little differently.
For example say that I want to use Math.round to create a new array of numbers from an array of numbers where each number is a fraction. In order to use invoke map I can not just pass Math.round as the second argument for invoke map, I will instead want to pass a function of my own. Also when passing a function of my own I will want to use a function expression, and not an arrow function because of the way that function expressions handle the value of the this keyword. I can then use the this keyword in the body of the function expression that I give to refer to the current element in the array, which in this case is a number, so I can then pass that as an argument for Math.round. The return value of Math.round will then need to be the return value of this function expression that I give to lodash invoke map.
So then that is a little involved, but I can still do this sort of thing with invoke map. However it might often be a little easier to just use the lodash map method in this case. With lodash map I can just pass the Math.round method as the second argument and be done with it.
So it is true that invokeMap is one of the many so called collection methods in lodash. That is it is a method that will not just work with arrays or objects in general but both. So if I have a similar situation as before, but now it is an object with named rather than numbers key values then I can still juts use invokeMap. The only difference now is that because it is a plain old javaScriot object it does not have sort in the prototype object, however I can pass the method as the second argument rather than a string that is a path to the method.
I am not the kind of developer that things that lodash is just filled with methods like invokeMap. There are many talking points as to why it is that lodash is more than just a collection of methods that I may or may not use in a project. Even if you do not use lodash it is still a good idea to look at the lodash source code as there is a lot to be learned as to how it is designed. Also there is much more to write about when it comes to why it might be a good idea to use some kind of user space module that is a collection of independent methods in its own global variable rather than monkey patching methods that should be there into native objects.
Still when it comes to invokeMap alone I can not say this is the most compelling method to support a case to use lodash. There are some good talking points as to why one should use lodash, or some kind of custom lodash like library though. With that said if you liked this post and would like to read more on lodash there is my main blog post on lodash that I started, and edit every now and then, and also a whole bunch of other posts that I have wrote on lodash over the years.