-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce2.js
39 lines (33 loc) · 1.34 KB
/
reduce2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const worlds = [
{ id: "1", city: "Jakarta", country: "Indonesia", population: 23 },
{ id: "2", city: "Tokyo", country: "Japan", population: 14 },
{ id: "3", city: "New York", country: "United States", population: 25 }
];
// Menghitung jumlah Object Property
result = worlds.reduce(acc => acc + 1, 0);
console.log(result); // ---- output: 3
// Mengihitung jumlah seluruh population
result = worlds.reduce((acc, pop) => acc + pop.population, 0);
console.log(result); // ---- output: 62
// Menampilkan semua city
result = worlds.reduce((acc, cty) => [...acc, cty.city], []);
console.log(result); // ---- output: [ 'Jakarta', 'Tokyo', 'New York' ]
// Menampilkan population terbanyak
result = worlds.reduce((acc, world) => {
if (acc === null || world.population > acc) return world.population;
return acc;
}, null);
console.log(result); // ---- output: 25
// Menampilkan population terkecil
result = worlds.reduce((acc, world) => {
if (acc === null || world.population < acc) return world.population;
return acc;
}, null);
console.log(result); // ---- output: 14
// Menampilkan object dengan property city "Tokyo"
result = worlds.reduce((acc, world) => {
if (acc !== null) return acc;
if (world.city === "Tokyo") return world;
return null;
}, null);
console.log(result); // ---- output: { id: '2', city: 'Tokyo', country: 'Japan', population: 14 }