Sobes.tech
Junior — Middle

What is the main purpose of the Map method and how is it used when working with data collections?

sobes.tech AI

Answer from AI

The map method is used to transform each element of a collection (such as an array) into a new element, forming a new array of the same length. The main task is to apply a function to each element of the original array and obtain an array of results.

Example in JavaScript:

const numbers = [1, 2, 3, 4];
const squares = numbers.map(x => x * x);
console.log(squares); // [1, 4, 9, 16]

This is useful when working with data, to transform or extract specific values from a collection without modifying the original.