- Underscore.JS Tutorial
- Underscore.JS - Home
- Underscore.JS - Overview
- Underscore.JS - Environment Setup
- Underscore.JS - Iterating Collection
- Underscore.JS - Processing Collection
- Underscore.JS - Iterating Array
- Underscore.JS - Processing Array
- Underscore.JS - Functions
- Underscore.JS - Mapping Objects
- Underscore.JS - Updating Objects
- Underscore.JS - Comparing Objects
- Underscore.JS - Utilities
- Underscore.JS - Chaining
- Underscore.JS Useful Resources
- Underscore.JS - Quick Guide
- Underscore.JS - Useful Resources
- Underscore.JS - Discussion
Underscore.JS - map method
Syntax
_.map(list, iteratee, [context])
map method produces a new array of values by mapping each value of list while iterating over a given list of element, call the iteratee function which is bound to context object, if passed. Iteratee is called with three parameters: (element, index, list). In case of JavaScript object, iteratee's object will be (value, key, list). Returns the list for chaining purpose.
Example
var _ = require('underscore'); //Example 1. get Square of each number of array var list = _.map([1, 2, 3], function(x) { return x*x }); console.log(list); //Example 2. get squre of each number of object list = _.map({one: 1, two: 2, three: 3}, function(value, key) { return value*value }); console.log(list);
Save the above program in tester.js. Run the following command to execute this program.
Command
\>node tester.js
Output
[ 1, 4, 9 ] [ 1, 4, 9 ]
underscorejs_iterating_collection.htm
Advertisements
To Continue Learning Please Login
Login with Google