To use the mapObject()
function from the Underscore library in JavaScript, first, we need to include the Underscore library in our project. We can do that by adding the following script
tag in our HTML file:
102 chars2 lines
Then we can use the mapObject()
function as follows:
index.tsx173 chars9 lines
In this example, we passed an object obj
to the mapObject()
function along with a callback function. The callback function takes two arguments, val
and key
, which represent the value and key of each property in the object. In the callback function, we multiplied the value of each property by 2
and returned the new value.
The mapObject()
function returned a new object with the same keys as the original object, but with the values returned by the callback function.
Thus, we can use the mapObject()
function from the Underscore library to apply a function to each property of an object and return a new object with the transformed values.
gistlibby LogSnag