-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-arrays-objects.js
More file actions
91 lines (71 loc) · 2.5 KB
/
Copy path05-arrays-objects.js
File metadata and controls
91 lines (71 loc) · 2.5 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* MATERI: ARRAYS & OBJECTS (DEEP DIVE)
* Mengelola struktur data kompleks dan teknik manipulasi modern.
*/
// --- 1. ARRAYS (MANIPULASI DATA LIST) ---
const fruits = ["Apple", "Mango", "Banana"];
// A. Menambah & Menghapus Elemen (Basic)
fruits.push("Orange"); // Tambah di akhir
fruits.unshift("Lemon"); // Tambah di awal
fruits.pop(); // Hapus dari akhir
fruits.shift(); // Hapus dari awal
// B. Mencari Data (Modern)
const hasApple = fruits.includes("Apple"); // true/false
const mangoIndex = fruits.indexOf("Mango");
const findFruit = fruits.find((f) => f.startsWith("M")); // Cari elemen spesifik
// C. Slice vs Splice (Penting!)
// Slice: Mengambil sebagian tanpa mengubah array asli
const someFruits = fruits.slice(0, 2);
// Splice: Mengubah array asli (Hapus, Tambah, atau Ganti)
// (index, jumlah_yang_dihapus, item_baru)
fruits.splice(1, 1, "Watermelon");
console.log("Final Fruits Array:", fruits);
// --- 2. OBJECTS (DATA TERSTRUKTUR & REFERENSI) ---
const user = {
id: 1,
username: "dev_user",
profile: {
fullName: "Budi Santoso",
address: "Jakarta, Indonesia",
},
hobbies: ["Coding", "Gaming"],
// Method di dalam Object
greet() {
return `Halo, saya ${this.username}`;
},
};
// A. Accessing Data
console.log(user.profile.fullName); // Dot notation
console.log(user["username"]); // Bracket notation
// B. Optional Chaining (?.) - ANTI ERROR!
// Sangat penting jika data dari API mungkin kosong/null
console.log("Social Media:", user.social?.instagram); // undefined, bukan error
// C. Object Shorthand & Computed Properties
const category = "tech";
const product = {
id: 101,
category, // Sama dengan category: category
[`item_${category}`]: "Laptop", // Dynamic Key
};
// --- 3. MODERN SPREAD & DESTRUCTURING ---
// A. Copy & Merge Array (Immutable)
const moreFruits = ["Grape", "Pineapple"];
const allFruits = [...fruits, ...moreFruits]; // Spread Operator
// B. Copy & Update Object
const updatedUser = {
...user,
username: "super_dev", // Override nilai lama
status: "Active", // Tambah property baru
};
// --- 4. DATA TRANSFORMATION (Sangat Sering Digunakan) ---
const products = [
{ id: 1, name: "Laptop", price: 1000 },
{ id: 2, name: "Mouse", price: 20 },
{ id: 3, name: "Monitor", price: 200 },
];
// Mengambil daftar nama saja (Mapping)
const productNames = products.map((p) => p.name);
// Filter produk mahal (> 100)
const expensiveProducts = products.filter((p) => p.price > 100);
console.log("Names:", productNames);
console.log("Expensive:", expensiveProducts);