Created
September 28, 2016 20:45
-
-
Save danielpowell4/8f6e7bb0734fb387ab564825df8897cd to your computer and use it in GitHub Desktop.
Uses a callback and loop to implement the map functionality of lodash and ruby in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var _ = { | |
// Implements: | |
// https://lodash.com/docs#map | |
map: (array, callback) => { | |
var newArray = [] | |
array.forEach(function(item){ | |
newArray.push(callback(item)) | |
}) | |
return newArray | |
} | |
} | |
// Define a method that multiplies the input by 10 | |
function multiplyBy10(value) { | |
return 10 * value; | |
} | |
const value = _.map([1, 2, 3], multiplyBy10); | |
// value => [10, 20, 30] | |
console.log(value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment