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 numbers = [0,1,2,3];
2
3console.log(numbers.map((number) => {
4 return number;
5}));
1// make new array from edited items of another array
2var newArray = unchangedArray.map(function(item, index){
3 return item; // do something to returned item
4});
5
6// same in ES6 style (IE not supported)
7var newArray = unchangedArray.map((item, index) => item);
1const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];
2
3// Appends text to each element of the array
4const newArray = myArray.map(name => {
5 return 'My name is ' + name;
6});
7console.log(newArray); // ['My name is Sam', 'My Name is Alice', ...]
8
9// Appends the index of each element with it's value
10const anotherArray = myArray.map((value, index) => index + ": " + value);
11console.log(anotherArray); // ['0: Sam', '1: Alice', '2: Nick', ...]
12
13// Starting array is unchanged
14console.log(myArray); // ['Sam', 'Alice', 'Nick', 'Matt']
1.wp-block-code {
2 border: 0;
3 padding: 0;
4}
5
6.wp-block-code > div {
7 overflow: auto;
8}
9
10.shcb-language {
11 border: 0;
12 clip: rect(1px, 1px, 1px, 1px);
13 -webkit-clip-path: inset(50%);
14 clip-path: inset(50%);
15 height: 1px;
16 margin: -1px;
17 overflow: hidden;
18 padding: 0;
19 position: absolute;
20 width: 1px;
21 word-wrap: normal;
22 word-break: normal;
23}
24
25.hljs {
26 box-sizing: border-box;
27}
28
29.hljs.shcb-code-table {
30 display: table;
31 width: 100%;
32}
33
34.hljs.shcb-code-table > .shcb-loc {
35 color: inherit;
36 display: table-row;
37 width: 100%;
38}
39
40.hljs.shcb-code-table .shcb-loc > span {
41 display: table-cell;
42}
43
44.wp-block-code code.hljs:not(.shcb-wrap-lines) {
45 white-space: pre;
46}
47
48.wp-block-code code.hljs.shcb-wrap-lines {
49 white-space: pre-wrap;
50}
51
52.hljs.shcb-line-numbers {
53 border-spacing: 0;
54 counter-reset: line;
55}
56
57.hljs.shcb-line-numbers > .shcb-loc {
58 counter-increment: line;
59}
60
61.hljs.shcb-line-numbers .shcb-loc > span {
62 padding-left: 0.75em;
63}
64
65.hljs.shcb-line-numbers .shcb-loc::before {
66 border-right: 1px solid #ddd;
67 content: counter(line);
68 display: table-cell;
69 padding: 0 0.75em;
70 text-align: right;
71 -webkit-user-select: none;
72 -moz-user-select: none;
73 -ms-user-select: none;
74 user-select: none;
75 white-space: nowrap;
76 width: 1%;
77}let map = new Map([iterable]);
78Code language: JavaScript (javascript)
1// The map() method creates a new array populated with the
2// results of calling a provided function on every element
3// in the calling array.
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]
1The map() method creates a new array with the results of calling a provided function on every element in the calling array.