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 }; });
1const array = [2, 5, 9];
2let squares = array.map((num) => num * num);
3
4console.log(array); // [2, 5, 9]
5console.log(squares); // [4, 25, 81]
1const map = new Map();
2
3function foo() {
4 return "Hello World!";
5}
6
7map.set("foo", foo);
8
9console.log(map.get("foo")()); // Output: "Hello World!
1The map() method creates a new array with the results of calling a provided function on every element in the calling array.