Skip to content

Instantly share code, notes, and snippets.

@trooperandz
Last active May 31, 2018 20:47
Show Gist options
  • Save trooperandz/5ca9d217204188bff9a78d87cebf8efa to your computer and use it in GitHub Desktop.
Save trooperandz/5ca9d217204188bff9a78d87cebf8efa to your computer and use it in GitHub Desktop.
Custom map prototype
// 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