xxxxxxxxxx
// Map objects are collections of key-value pairs.
// A key in the Map may only occur once, it is unique in the Map 's collection
let map = new Map()
let keyArray = 'Array'
map.set(keyArray, [1, 2, 3, 4, 5])
map.get(keyArray) // 1, 2, 3, 4, 5
xxxxxxxxxx
array.map((item) => {
return item * 2
} // an example that will map through a a list of items and return a new array with the item multiplied by 2
xxxxxxxxxx
const numbers = [1, 2, 3, 4, 5];
const bigNumbers = numbers.map(number => {
return number * 10;
});
xxxxxxxxxx
const numbers = [1, 4, 9];
const roots = numbers.map((num) => Math.sqrt(num));
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
xxxxxxxxxx
const numbers = [0,1,2,3];
console.log(numbers.map((number) => {
return number;
}));
xxxxxxxxxx
var numbers = [1, 4, 9];
var doubles = numbers.map(function(num) {
return num * 2;
});
// doubles is now [2, 8, 18]. numbers still [1, 4, 9]
xxxxxxxxxx
// make new array from edited items of another array
var newArray = unchangedArray.map(function(item, index){
return item; // do something to returned item
});
// same in ES6 style (IE not supported)
var newArray2 = unchangedArray.map((item, index) => item);
xxxxxxxxxx
const numbers = [2, 4, 6, 8];
const result = numbers.map(a => a * 2);
console.log(result);
//Output: [ 4, 8, 12, 16 ]
xxxxxxxxxx
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const anotherItems = items.map(item => item * 2);
console.log(anotherItems);
xxxxxxxxxx
//forEach vs. Map High Order Array Methods
const arr = [1,2,3]
const arr2 = [arr,4,5]//see spread operator grepper answer for [...arr]
//the forEach doesn't return anything, it just loops through the array
arr2.forEach(function(item) {
console.log(item + ' of ' + arr2.length)
})
//map allows us to return an array and store it into a new array
const arr3 = arr2.map(function(item) {
//after performing some operation on all the objects of the array
//we can then return all those values into a new array
return item * 2
})
console.log(arr3)