Tarikh: 13 Disember 2024
Selamat datang ke Hari ke-6 perjalanan JavaScript anda! Hari ini, kami menyelidiki dua struktur data penting dalam JavaScript: Array dan Objek. Struktur ini membentuk tulang belakang manipulasi data dalam pembangunan web moden. Dengan menguasai tatasusunan dan objek, anda akan membuka kunci cara yang berkuasa untuk menyimpan, mengakses dan mengubah data dengan cekap.
tatasusunan ialah koleksi item (dipanggil elemen) yang disimpan di lokasi memori bersebelahan. Dalam JavaScript, tatasusunan adalah serba boleh dan boleh menyimpan jenis data bercampur.
// Empty array let emptyArray = []; // Array with initial elements let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits); // Output: ["Apple", "Banana", "Cherry"] // Mixed data types let mixedArray = [42, "Hello", true]; console.log(mixedArray); // Output: [42, "Hello", true]
Contoh:
let numbers = [1, 2, 3]; numbers.push(4); // Adds 4 to the end console.log(numbers); // Output: [1, 2, 3, 4] numbers.pop(); // Removes the last element console.log(numbers); // Output: [1, 2, 3] numbers.unshift(0); // Adds 0 to the beginning console.log(numbers); // Output: [0, 1, 2, 3] numbers.shift(); // Removes the first element console.log(numbers); // Output: [1, 2, 3]
let nums = [1, 2, 3, 4]; let squares = nums.map(num => num * num); console.log(squares); // Output: [1, 4, 9, 16]
let ages = [12, 18, 22, 16]; let adults = ages.filter(age => age >= 18); console.log(adults); // Output: [18, 22]
let numbers = [1, 2, 3, 4]; let sum = numbers.reduce((acc, curr) => acc + curr, 0); console.log(sum); // Output: 10
Sebuah objek ialah koleksi sifat, di mana setiap sifat adalah pasangan nilai kunci. Objek sesuai untuk mewakili entiti dunia sebenar dengan atribut.
let person = { name: "Arjun", age: 22, isStudent: true, }; console.log(person.name); // Output: Arjun console.log(person["age"]); // Output: 22
let car = { brand: "Tesla", }; car.model = "Model 3"; // Adding a new property car.brand = "Ford"; // Updating a property console.log(car); // Output: { brand: "Ford", model: "Model 3" }
delete car.model; console.log(car); // Output: { brand: "Ford" }
Contoh:
// Empty array let emptyArray = []; // Array with initial elements let fruits = ["Apple", "Banana", "Cherry"]; console.log(fruits); // Output: ["Apple", "Banana", "Cherry"] // Mixed data types let mixedArray = [42, "Hello", true]; console.log(mixedArray); // Output: [42, "Hello", true]
Feature | Arrays | Objects |
---|---|---|
Storage | Ordered collection of items. | Unordered collection of key-value pairs. |
Access | Index-based (arr[0]). | Key-based (obj.key). |
Best Use Case | List of related items. | Grouping attributes of an entity. |
let numbers = [1, 2, 3]; numbers.push(4); // Adds 4 to the end console.log(numbers); // Output: [1, 2, 3, 4] numbers.pop(); // Removes the last element console.log(numbers); // Output: [1, 2, 3] numbers.unshift(0); // Adds 0 to the beginning console.log(numbers); // Output: [0, 1, 2, 3] numbers.shift(); // Removes the first element console.log(numbers); // Output: [1, 2, 3]
let nums = [1, 2, 3, 4]; let squares = nums.map(num => num * num); console.log(squares); // Output: [1, 4, 9, 16]
let ages = [12, 18, 22, 16]; let adults = ages.filter(age => age >= 18); console.log(adults); // Output: [18, 22]
Langkah Seterusnya
DalamAtas ialah kandungan terperinci Meneroka Tatasusunan dan Objek dalam JavaScript. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!