xxxxxxxxxx
const myMap = new Map();
let userid = 1314124141;
myMap.set('veri_' + userid, 1) // set
myMap.set('veri_' + userid, myMap.get('veri_' + userid) + 1) // push
console.log(myMap.get('veri_' + userid)) // output: 3
xxxxxxxxxx
The map() method creates a new array populated with the results of calling
a provided function on every element in the calling array.
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]
xxxxxxxxxx
function map(array, transform) {
let mapped = [];
for (let element of array) {
mapped.push(transform(element));
}
return mapped;
}
let rtlScripts = SCRIPTS.filter(s => s.direction == "rtl");
console.log(map(rtlScripts, s => s.name));
// → ["Adlam", "Arabic", "Imperial Aramaic", …]
xxxxxxxxxx
var number = [1, 2 , 3, 4, 5, 6, 7, 8, 9];
var doubles = number.map((n) => { return n*2 })
/* doubles
(9) [2, 4, 6, 8, 10, 12, 14, 16, 18] */
xxxxxxxxxx
let users = [
{ firstName: "Susan", lastName: "Steward", age: 14, hobby: "Singing" },
{ firstName: "Daniel", lastName: "Longbottom", age: 16, hobby: "Football" },
{ firstName: "Jacob", lastName: "Black", age: 15, hobby: "Singing" }
];
xxxxxxxxxx
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
xxxxxxxxxx
# map function
city_lengths = map(len, ['sanjose', 'cupertino', 'sunnyvale', 'fremont'])
print(list(city_lengths))
# [7, 9, 9, 7]
# Map takes a function and a collection of items. It makes a new, empty collection, runs the function on each item in the original collection and inserts each return value into the new collection. It returns the new collection.
xxxxxxxxxx
const ratings = watchList.map(item => ({
title: item["Title"],
rating: item["imdbRating"]
}));
xxxxxxxxxx
numbers.map(n => n * 2)
// No curly braces = implicit return
// Same as: numbers.map(function (n) { return n * 2 })
numbers.map(n => ({
result: n * 2
}))
// Implicitly returning objects requires parentheses around the object