Skip to content

Instantly share code, notes, and snippets.

@danielnass
Created October 11, 2022 01:43
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 danielnass/8e002bf0828ddebe30fc7564b9174a6a to your computer and use it in GitHub Desktop.
Save danielnass/8e002bf0828ddebe30fc7564b9174a6a to your computer and use it in GitHub Desktop.
JavaScript Array Map From Scratch
// 1 - Create map function
function map(array, fn){
let newArr = [];
for (value of array){
newArr.push(fn(value));
}
return newArr;
}
// 2 - Create an array to iterate into map
const myArr = [1,2,3];
// 3 - The callback function to modify the value of each map iteration
function doubleIt(value){
return value * 2;
}
// 4 - Execute the map function with myArr and doubleIt and return it to doubleArrayValues const
const doubleArrayValues = map(myArr, doubleIt); // [2,4,6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment