Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaScript Map() Method

The JavaScript map() method transforms an array by applying a callback function to each element, resulting in a new array with modified values. It takes a callback function as a parameter, which can access the current element, its index, and the original array. Examples include doubling numbers in an array and extracting properties from objects.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaScript Map() Method

The JavaScript map() method transforms an array by applying a callback function to each element, resulting in a new array with modified values. It takes a callback function as a parameter, which can access the current element, its index, and the original array. Examples include doubling numbers in an array and extracting properties from objects.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

JavaScript map() Method

The map() method is used to transform an array by applying a function to each


element and returning a new array with the modified values.

Syntax
js
Copy
Edit
const newArray = array.map(callback(currentValue, index, array));
Parameters
callback(currentValue, index, array)

currentValue → The current element being processed.


index (optional) → The index of the current element.
array (optional) → The original array.
Returns: A new array with transformed elements.

Examples
Example 1: Doubling Numbers
js
Copy
Edit
const numbers = [1, 2, 3, 4, 5];

const doubled = numbers.map(num => num * 2);

console.log(doubled); // Output: [2, 4, 6, 8, 10]


✅ Each element is multiplied by 2, returning a new array.

Example 2: Extracting Property from Objects


js
Copy
Edit
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 22 }
];

const names = users.map(user => user.name);

console.log(names); // Output: ["Alice", "Bob", "Charlie"]


✅ Extracts only the name property from an array of objects.

You might also like