Skip to content

Instantly share code, notes, and snippets.

@bmacmill
Created April 24, 2017 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bmacmill/76e4b92d2cddbafd8f01713f59d45f7d to your computer and use it in GitHub Desktop.
Save bmacmill/76e4b92d2cddbafd8f01713f59d45f7d to your computer and use it in GitHub Desktop.
03 - Annotating _.map
//we create an object called _
var _ = {
//the first property is called map it's a method that takes two arguments
//the first argument is list, the second is a call back function
map: function(list, cbFn) {
//this funciton creates a new array
var newArray = [];
//it uses a forEach loop to go through list returning the element i'm not sure what index is doing?
list.forEach(function(elem, index) {
//im not sure why it's setting the cbFn as a variable here instead
//of just putting the cbfn into the new Array
var newElem = cbFn(elem, index);
//here it pushes the neweEleme variable into the new Array
newArray.push(newElem);
});
//here it returns the new Array
return newArray;
}
};
//the method is invoked passing in the array 1,2,3 and a new function that multiplies the 1,2,3 by 3
//reading the underscore.js documentationo, i get that it is "transforming" the array passed in, but I'm failing to see how this is helpful right now...
//it looks like it's an array passin into an array.
//in my defense i put my thumb up sideways as far as how I felt on this!!
_.map([1, 2, 3], function(num){ return num * 3; });
// => [3, 6, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment