Skip to content

Instantly share code, notes, and snippets.

@Fraasi
Created October 9, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fraasi/9c338aa315a93f2a7371cc3db06c6244 to your computer and use it in GitHub Desktop.
Save Fraasi/9c338aa315a93f2a7371cc3db06c6244 to your computer and use it in GitHub Desktop.
Rewriting map function
Array.prototype.myMap = function (callback, thisArg) {
if (!thisArg) thisArg = this
if (!Array.isArray(thisArg)) throw new Error('Not an Array')
if (typeof callback !== 'function') throw new Error('Callback must be a function')
const newArray = []
for (let i = 0; i < thisArg.length; i++) {
newArray.push(callback(thisArg[i], i, thisArg))
}
return newArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment