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: array map javascript
const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
Example 4: functions in map javascript
const map = new Map();
function foo() {
return "Hello World!";
}
map.set("foo", foo);
console.log(map.get("foo")());
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: map javascript
var miMapa = new Map();
var claveObj = {},
claveFunc = function () {},
claveCadena = "una cadena";
miMapa.set(claveCadena, "valor asociado con 'una cadena'");
miMapa.set(claveObj, "valor asociado con claveObj");
miMapa.set(claveFunc, "valor asociado with claveFunc");
miMapa.size;
miMapa.get(claveCadena);
miMapa.get(claveObj);
miMapa.get(claveFunc);
miMapa.get("una cadena");
miMapa.get({});
miMapa.get(function() {})
var myMap = new Map();
myMap.set("bar", "foo");
myMap.delete("bar");
myMap.has("bar");