8000 array foreach · last-endcode/JavaScript-Basics@1ada1f1 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 1ada1f1

Browse files
committed
array foreach
1 parent 4b562af commit 1ada1f1

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

35_array_foreach.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22
// doesn't return new array
33
// method executes a provided function once for each array element
44

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' },
99
];
1010

11-
//disini kita bisa memanggil dgn 2 cara..
11+
// let person = function (item) {
12+
// console.log(item);
13+
// };
1214

13-
function showMember(memberBajakLaut) {
14-
console.log(memberBajakLaut.position.toUpperCase());
15-
}
15+
// people.forEach(person);
1616

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+
);
2224
});

0 commit comments

Comments
 (0)
0