Skip to content

Instantly share code, notes, and snippets.

@danielpowell4
Created September 28, 2016 20:45
Show Gist options
  • Save danielpowell4/8f6e7bb0734fb387ab564825df8897cd to your computer and use it in GitHub Desktop.
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
"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