lodash Use _.map to Transform a List


Example

_.map is useful for changing a list into a different list in a purely declarative way. Rather than using imperative techniques like a while or for loop in javascript, you can just specify how you want to manipulate an element of a list and

Use _.map to make a new list transformed by the function you provide.

Let's say we want to square all the numbers in a list. First we'll create a list using the _.range function:

var a = _.range(10);       // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Now we'll create a list of squares by using _.map:

var b = _.map(a, function(e){ return e * e;} );
// b is now [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]