Map
Learn how `Array.map` works by learning how to write it yourself. We'll see what makes this function so powerful and how it allows us to work efficiently with large amounts of data. You'll use this functions thousands of times in your JavaScript career.
We'll cover the following...
Array.map
Learn how Array.map
works by writing it yourself.
This function allows us to manipulate data effectively and efficiently. It’s meant to copy an array and change it a little bit in the process. You’ll be using it for the rest of your JavaScript career.
Array.map
is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a reference to a new array.
Because the original data source, the original array, is left untouched, Array.map
is a pure function. It’s the first of a few methods that will allow us to really use functional programming to perform useful tasks, without feeling like we’re adding complexity.
Introduction
Let’s say we have an array of numbers and we want to multiply each number by three. We also don’t want to change the original array. To do this without Array.map
, we can use a standard for-loop.
for
-loop
const originalArr = [1, 2, 3, 4, 5];const newArr = [];for(let i = 0; i < originalArr.length; i++) {newArr[i] = originalArr[i] * 3;}console.log(newArr); // -> [3, 6, 9, 12, 15]
Simple enough. Let’s abstract this ...
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy