javascript / beginner
Snippet
Array Map Method
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. It is a fundamental tool for data transformation.
snippet.js
1
2
3
4
const numbers = [1, 2, 3];const squared = numbers.map(num => num * num);console.log(squared);
nodejs
Breakdown
1
const squared = numbers.map(num => num * num);
Iterates through 'numbers' and returns a new array where each number is squared.
2
console.log(squared);
Outputs the new transformed array: [1, 4, 9].