Last active
May 31, 2018 20:47
-
-
Save trooperandz/5ca9d217204188bff9a78d87cebf8efa to your computer and use it in GitHub Desktop.
Custom map prototype
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
// Create our custom map function and attach it to the prototype | |
Array.prototype.customMap = function(callback) { | |
const arr = this; | |
const newArr = []; | |
for(let i = 0; i < arr.length; i++) { | |
const alteredElement = callback(arr[i], i, arr); | |
newArr.push(alteredElement); | |
} | |
return newArr; | |
} | |
// Execute our custom map function | |
const beachArr = ['waves', 'sharks', 'jellies', 'sand', 'pelicans']; | |
const newArr = beachArr.customMap(item => item.substring(0, 1)); | |
// ['w', 's', 'j', 's', 'p'] | |
console.log(`Our newly mapped array: ${newArr}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment