map js syntax code example

Example 1: javascript map array

const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];

// Appends text to each element of the array
const newArray = myArray.map(name => {
	return 'My name is ' + name; 
});
console.log(newArray); // ['My name is Sam', 'My Name is Alice', ...]

// Appends the index of each element with it's value
const anotherArray = myArray.map((value, index) => index + ": " + value);
console.log(anotherArray); // ['0: Sam', '1: Alice', '2: Nick', ...]

// Starting array is unchanged
console.log(myArray); // ['Sam', 'Alice', 'Nick', 'Matt']

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..." },
];
// ES2016+
// Create new array of post IDs. I.e. [1,2,3]
const postIds = posts.map((post) => post.id);
// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
const postSummaries = posts.map((post) => ({ id: post.id, title: post.title }));

// ES2015
// Create new array of post IDs. I.e. [1,2,3]
var postIds = posts.map(function (post) { return post.id; });
// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
var postSummaries = posts.map(function (post) { return { id: post.id, title: post.title }; });

Example 3: array map javascript

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

Example 4: functions in map javascript

const map = new Map();

function foo() {
 return "Hello World!"; 
}

map.set("foo", foo);

console.log(map.get("foo")()); // Output: "Hello World!

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";

// asignando valores
miMapa.set(claveCadena, "valor asociado con 'una cadena'");
miMapa.set(claveObj, "valor asociado con claveObj");
miMapa.set(claveFunc, "valor asociado with claveFunc");

miMapa.size; // 3

// obteniendo los valores
miMapa.get(claveCadena);    // "valor asociado con 'una cadena'"
miMapa.get(claveObj);       // "valor asociado con claveObj"
miMapa.get(claveFunc);      // "valor asociado con claveFunc"

miMapa.get("una cadena");   // ""valor asociado con 'una cadena'"
                         // porque claveCadena === 'una cadena'
miMapa.get({});           // undefined, porque claveObj !== {}
miMapa.get(function() {}) // undefined, porque claveFunc !== function () {}

var myMap = new Map();
myMap.set("bar", "foo");

myMap.delete("bar"); // Retorna true. Eliminado con éxito.
myMap.has("bar");    // Retorna false. El elemento "bar" ya no está presente.