Skip to content

Instantly share code, notes, and snippets.

@trooperandz
Created May 31, 2018 20:28
Show Gist options
  • Save trooperandz/e1430a6d36798fc09ddf88c1e6640489 to your computer and use it in GitHub Desktop.
Save trooperandz/e1430a6d36798fc09ddf88c1e6640489 to your computer and use it in GitHub Desktop.
Custom forEach prototype
// Create our custom forEach function and attach it to the prototype
Array.prototype.customForEach = function(callback) {
const arr = this;
for(let i = 0; i < arr.length; i++) {
callback(arr[i], i, arr);
}
}
// Execute our custom forEach function
const beachArr = ['waves', 'sharks', 'jellies', 'sand', 'pelicans'];
beachArr.customForEach((item, i, arr) => {
console.log(`
Current array element: ${item} \n
Current array index: ${i} \n
The whole array: ${arr} \n
`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment