1array.map((item) => {
2 return item * 2
3} // an example that will map through a a list of items and return a new array with the item multiplied by 2
1const numbers = [1, 2, 3, 4, 5];
2
3const bigNumbers = numbers.map(number => {
4 return number * 10;
5});
1The map() method creates a new array populated with the results of calling
2a provided function on every element in the calling array.
3
4const array1 = [1, 4, 9, 16];
5
6// pass a function to map
7const map1 = array1.map(x => x * 2);
8
9console.log(map1);
10// expected output: Array [2, 8, 18, 32]
1let myMap = new Map()
2
3let keyString = 'a string'
4let keyObj = {}
5// setting the values
6myMap.set(keyString, "value associated with 'a string'")
7myMap.set(keyObj, 'value associated with keyObj')
8myMap.set(keyFunc, 'value associated with keyFunc')
9myMap.size // 3
10// getting the values
11myMap.get(keyString) // "value associated with 'a string'"
12myMap.get(keyObj) // "value associated with keyObj"
13myMap.get(keyFunc) // "value associated with keyFunc"
1function map(array, transform) {
2 let mapped = [];
3 for (let element of array) {
4 mapped.push(transform(element));
5 }
6 return mapped;
7}
8
9let rtlScripts = SCRIPTS.filter(s => s.direction == "rtl");
10console.log(map(rtlScripts, s => s.name));
11// → ["Adlam", "Arabic", "Imperial Aramaic", …]