1var myObject = { 'a': 1, 'b': 2, 'c': 3 };
2
3Object.keys(myObject).map(function(key, index) {
4 myObject[key] *= 2;
5});
6
7console.log(myObject);
8// => { 'a': 2, 'b': 4, 'c': 6 }
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 }; });
1function listFruits() {
2 let fruits = ["apple", "cherry", "pear"]
3
4 fruits.map((fruit, index) => {
5 console.log(index, fruit)
6 })
7}
8
9listFruits()
10
11// https://jsfiddle.net/tmoreland/16qfpkgb/3/
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]
8
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]
1// A function which returns the last character of a given string
2function returnLast(arr) {
3 return arr.at(-1);
4}
5
6let invoiceRef = 'myinvoice01';
7
8console.log( returnLast(invoiceRef) );
9// Logs: '1'
10
11invoiceRef = 'myinvoice02';
12
13console.log( returnLast(invoiceRef) );
14// Logs: '2'
15