Created
October 11, 2022 01:43
-
-
Save danielnass/8e002bf0828ddebe30fc7564b9174a6a to your computer and use it in GitHub Desktop.
JavaScript Array Map From Scratch
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
// 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