js map arrays code example
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: how the map function works javascript
const array = [2, 5, 9];
let squares = array.map((num) => num * num);
console.log(array);
console.log(squares);
Example 3: map javascript
var kvArray = [["clave1", "valor1"], ["clave2", "valor2"]];
var miMapa = new Map(kvArray);
miMapa.get("clave1");
console.log(Array.from(miMapa));
console.log(Array.from(miMapa.keys()));
Example 4: 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");