The collect
function in the Underscore library is also known as map
. It applies a mapping function to each element of an array and returns a new array with the modified elements. Here's an example:
index.tsx203 chars10 lines
In this example, the _.map
function takes two arguments: the array to be mapped (numbers
) and a callback function that takes each element of the array (num
) and returns the modified element (num * num
). The resulting array is stored in the squares
variable and displayed using console.log
.
Note: The collect
function is an alias for map
in Underscore. They both work the same way.
gistlibby LogSnag