1const posts = [
2 { id: 1, title: "Sample Title 1", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
3 { id: 2, title: "Sample Title 2", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
4 { id: 3, title: "Sample Title 3", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit..." },
5];
6// ES2016+
7// Create new array of post IDs. I.e. [1,2,3]
8const postIds = posts.map((post) => post.id);
9// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
10const postSummaries = posts.map((post) => ({ id: post.id, title: post.title }));
11
12// ES2015
13// Create new array of post IDs. I.e. [1,2,3]
14var postIds = posts.map(function (post) { return post.id; });
15// Create new array of post objects. I.e. [{ id: 1, title: "Sample Title 1" }]
16var postSummaries = posts.map(function (post) { return { id: post.id, title: post.title }; });
1// Use map to create a new array in memory. Don't use if you're not returning
2const arr = [1,2,3,4]
3
4// Get squares of each element
5const sqrs = arr.map((num) => num ** 2)
6console.log(sqrs)
7// [ 1, 4, 9, 16 ]
8
9//Original array untouched
10console.log(arr)
11// [ 1, 2, 3, 4 ]
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"
1const arr = [1, 2, 3, 4, 5, 6];
2const mapped = arr.map(el => el + 20); //[21, 22, 23, 24, 25, 26]
1['elem', 'another', 'name'].map((value, index, originalArray) => {
2 console.log(.....)
3});