|
2 | 2 | // doesn't return new array
|
3 | 3 | // method executes a provided function once for each array element
|
4 | 4 |
|
5 |
| -const memberSHP = [ |
6 |
| - { name: 'lutfi', age: 20, position: 'raja bajak laut' }, |
7 |
| - { name: 'zoro', age: 25, position: 'wakil raja bajak laut' }, |
8 |
| - { name: 'sanji', age: 24, position: 'chef SHP' }, |
| 5 | +const people = [ |
| 6 | + { name: 'bobo', born: 1987, position: 'developer' }, |
| 7 | + { name: 'peter', born: 1989, position: 'designer' }, |
| 8 | + { name: 'sussy', born: 1975, position: 'the boss' }, |
9 | 9 | ];
|
10 | 10 |
|
11 |
| -//disini kita bisa memanggil dgn 2 cara.. |
| 11 | +// let person = function (item) { |
| 12 | +// console.log(item); |
| 13 | +// }; |
12 | 14 |
|
13 |
| -function showMember(memberBajakLaut) { |
14 |
| - console.log(memberBajakLaut.position.toUpperCase()); |
15 |
| -} |
| 15 | +// people.forEach(person); |
16 | 16 |
|
17 |
| -// memberSHP.forEach(showMember); |
18 |
| - |
19 |
| -// cara ke-2 mnggunakan annonymous function |
20 |
| -memberSHP.forEach(function (items) { |
21 |
| - console.log(items.position.toLowerCase()); |
| 17 | +/* u can callback function with directly use annonymous function */ |
| 18 | +people.forEach(function (item) { |
| 19 | + let total = 2020; |
| 20 | + total -= item.born; |
| 21 | + console.log( |
| 22 | + `${item.name} with position ${item.position} now is ${total} years old` |
| 23 | + ); |
22 | 24 | });
|
0 commit comments