how to use the invokemap function from lodash in javascript

One way to use the invokeMap function from lodash is as follows:

index.tsx
_.invokeMap(collection, methodName, [args])
44 chars
2 lines

The invokeMap function applies the methodName function to each element in collection (an array or object), returning a new array of the results. Optional args can be passed to the method.

Here is an example of using invokeMap to get the uppercased version of each element of an array:

index.tsx
const strings = ['hello', 'world'];
const uppercasedStrings = _.invokeMap(strings, 'toUpperCase');
console.log(uppercasedStrings); // ['HELLO', 'WORLD']
153 chars
4 lines

In this example, we pass the strings array as the collection parameter, and 'toUpperCase' as the methodName parameter. The toUpperCase method is then applied to each element of the strings array, and the resulting array of uppercase strings is returned and stored in uppercasedStrings.

Note that lodash provides alternative syntaxes for invokeMap that allow for more flexibility in the way methodName is specified. Refer to the Lodash documentation for more information.

gistlibby LogSnag