The map
function from the Underscore JavaScript library is a higher-order function that applies a function to each element of an array and returns a new array of the results. The syntax for using map
in Underscore is as follows:
index.tsx33 chars2 lines
where list
is the array to be mapped, iteratee
is the function to apply to each element, and context
is an optional object that will be the value of this
in iteratee
.
Here is an example of using the map
function in Underscore:
index.tsx163 chars8 lines
In this example, map
applies the function function(num) { return num * num; }
to each element of the numbers
array and returns a new array containing the squared numbers.
You can also use an arrow function instead of a regular function expression:
index.tsx142 chars6 lines
Using the map
function from Underscore can help make your code more concise and expressive, and enable you to take advantage of functional programming concepts in JavaScript.
gistlibby LogSnag