Example 1: javascript map array
const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];
const newArray = myArray.map(name => {
return 'My name is ' + name;
});
console.log(newArray);
const anotherArray = myArray.map((value, index) => index + ": " + value);
console.log(anotherArray);
console.log(myArray);
Example 2: javascript map function
const posts = [
{ id: 1, title: "Sample Title 1", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
{ id: 2, title: "Sample Title 2", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
{ id: 3, title: "Sample Title 3", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
];
const postIds = posts.map((post) => post.id);
const postSummaries = posts.map((post) => ({ id: post.id, title: post.title }));
var postIds = posts.map(function (post) { return post.id; });
var postSummaries = posts.map(function (post) { return { id: post.id, title: post.title }; });
Example 3: map javascript
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
Example 4: javascript map
array.map( item => item.id )
array2.map( item => item.toString() )
array2.map( item => item * 3 )
Example 5: map function javascript
var rebels = pilots.filter(function (pilot) { return pilot.faction === "Rebels";});var empire = pilots.filter(function (pilot) { return pilot.faction === "Empire";});
Example 6: como fazer map em javascript
const apresentacao = ['Ilan', 'Daniela'];
apresentacao.map(nome => `Olá, ${nome}. Bem-vind@ :)`);
const apresentacao = [2, 4, 6, 8];
apresentacao.map(num => `${num}^2 = ${num**2}`)
(5) ["2^2 = 4", "4^2 = 16", "6^2 = 36", "8^2 = 64"]