Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Transforming Arrays with map | Transforming and Searching Arrays
JavaScript Array Methods

bookTransforming Arrays with map

The map method in JavaScript is used to transform each element of an array by applying a function to every item. It takes a callback function as its argument and returns a new array containing the results of calling that function on every element in the original array. Importantly, the original array is not changed. You should use map when you want to create a new array where each element is the result of some transformation of the corresponding element in the source array. Unlike forEach, which is used for executing side effects and does not return a value, map always returns a new array and is meant for data transformation.

12345
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(function(num) { return num * num; }); console.log(squares); // [1, 4, 9, 16, 25]
copy
123456789
const users = [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ]; const names = users.map(function(user) { return user.name; }); console.log(names); // ["Alice", "Bob", "Charlie"]
copy

The main difference between map and filter is in their output and intended use. The map method always returns a new array with the same number of elements as the original, but with each element transformed according to the callback function. The filter method, on the other hand, returns a new array containing only those elements that pass a test specified by the callback function, which means the resulting array may be shorter than the original.

  • Use map when you want to change every element;
  • Use filter when you want to select certain elements.

1. What does the map() method do?

2. Fill in the blank to complete the map callback that doubles each number in the array.

question mark

What does the map() method do?

Select the correct answer

question-icon

Fill in the blank to complete the map callback that doubles each number in the array.

const numbers = [2, 4, 6]; const doubled = numbers.map(function(num) { return ; }); console.log(doubled); // [4, 8, 12]
[4, 8, 12]

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 8.33

bookTransforming Arrays with map

Sveip for å vise menyen

The map method in JavaScript is used to transform each element of an array by applying a function to every item. It takes a callback function as its argument and returns a new array containing the results of calling that function on every element in the original array. Importantly, the original array is not changed. You should use map when you want to create a new array where each element is the result of some transformation of the corresponding element in the source array. Unlike forEach, which is used for executing side effects and does not return a value, map always returns a new array and is meant for data transformation.

12345
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(function(num) { return num * num; }); console.log(squares); // [1, 4, 9, 16, 25]
copy
123456789
const users = [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ]; const names = users.map(function(user) { return user.name; }); console.log(names); // ["Alice", "Bob", "Charlie"]
copy

The main difference between map and filter is in their output and intended use. The map method always returns a new array with the same number of elements as the original, but with each element transformed according to the callback function. The filter method, on the other hand, returns a new array containing only those elements that pass a test specified by the callback function, which means the resulting array may be shorter than the original.

  • Use map when you want to change every element;
  • Use filter when you want to select certain elements.

1. What does the map() method do?

2. Fill in the blank to complete the map callback that doubles each number in the array.

question mark

What does the map() method do?

Select the correct answer

question-icon

Fill in the blank to complete the map callback that doubles each number in the array.

const numbers = [2, 4, 6]; const doubled = numbers.map(function(num) { return ; }); console.log(doubled); // [4, 8, 12]
[4, 8, 12]

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt