Wednesday, July 27, 2011

Skilldrick » Easy functional programming in JavaScript with Underscore.js — part 1

Skilldrick » Easy functional programming in JavaScript with Underscore.js — part 1: "Mapping over an array
One of the most widely used Underscore functions is _.map. This takes an array and a transforming function, and creates a new array based on the return value of the transforming function. That’s a bit abstract so I’ll give you an example:


So, _.map is passed an array, and a function. That function is called on each element of the array in turn, and the return value of the function gives the corresponding element in the new array. Note that the original array is left intact. The _.map function takes an array, and returns another array – it’s a mapping from one value to another."