Underscore.js _.map() Function
The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects.
The _.map() function is an inbuilt function in Underscore.js library of the JavaScript which is used to produce a new array of values by mapping each value in list through transformation function (iteratee). It displays the result as a list on the console. In this list the output of the first element, i.e, the element at index 0 has it’s result at index 0 of the returned list and when all the elements of the list are passed to the function/iteratee and no more elements remain then the _.map loop ends.
Syntax:
_.map(list, function)
Parameters: It accepts two parameters which are specified below-
- list: It is the list which contains some elements.
- function: It is the function which is executed by taking each element of the list.
Return values: It returns the object property value or array element value at the current position.
< html > < head > < script > underscore.js/1.9.1/underscore-min.js" > </ script > < script type = "text/javascript" /1.9.1/underscore-min.js.map"></ script > < script type = "text/javascript" /1.9.1/underscore.js"></ script > </ head > < body > < script type = "text/javascript" > _.map([1, 2, 3, 4], function(num){ return num * 2; }); </ script > </ body > </ html > |
Output:
< html > < head > < script > underscore.js/1.9.1/underscore-min.js" > </ script > < script type = "text/javascript" /1.9.1/underscore-min.js.map"></ script > < script type = "text/javascript" /1.9.1/underscore.js"></ script > </ head > < body > < script type = "text/javascript" > _.map( [1, 2, 3], function( num ) { for(var i=0;i< num ;i++) var str = "This is" ; str+=i+1; str+="list item"; return(str); }); </script> </ body > </ html > |
Output:
< html > < head > < script > underscore.js/1.9.1/underscore-min.js" > </ script > < script type = "text/javascript" /1.9.1/underscore-min.js.map"></ script > < script type = "text/javascript" /1.9.1/underscore.js"></ script > </ head > < body > < script type = "text/javascript" > _.map([[1, 2], [3, 4], [5, 6]], _.last); </ script > </ body > </ html > |
Output:
< html > < head > < script > underscore.js/1.9.1/underscore-min.js" > </ script > < script type = "text/javascript" /1.9.1/underscore-min.js.map"></ script > < script type = "text/javascript" /1.9.1/underscore.js"></ script > </ head > < body > < script type = "text/javascript" > var list = ['Geeks','for', 'Geeks', 'JS']; m = _.map(list, function (l) { return l + ' is mapped from a list.'; }); </ script > </ body > </ html > |
Output:
< html > < head > < script > underscore.js/1.9.1/underscore-min.js" > </ script > < script type = "text/javascript" /1.9.1/underscore-min.js.map"></ script > < script type = "text/javascript" /1.9.1/underscore.js"></ script > </ head > < body > < script type = "text/javascript" > _.map( [ 0, 7, 2, -1, 8 ], function( n ) { return n>=3 ? "greater" : "smaller"; }); </ script > </ body > </ html > |
Output: