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 array1 = [1, 4, 9, 16];
2
3// pass a function to map
4const map1 = array1.map(x => x * 2);
5
6console.log(map1);
7// expected output: Array [2, 8, 18, 32]
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 sweetArray = [2, 3, 4, 5, 35]
2const sweeterArray = sweetArray.map(sweetItem => {
3 return sweetItem * 2
4})
5
6console.log(sweeterArray)
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]
1// Beispielarray
2const array1 = [1, 4, 9, 16];
3
4// Übergebe eine Funktion an die Karte
5const map1 = array1.map (x => x * 2);
6
7console.log (map1);
8// Erwartete Ausgabe: Array [2, 8, 18, 32]