⏱️ ~11 นาที

Array (อาร์เรย์)

เก็บข้อมูลหลายค่าเป็นชุดในตัวแปรเดียว

Array คือ "ตู้ลำโพงหลายช่อง"

ถ้าตัวแปรธรรมดาเปรียบเหมือนกล่องเก็บของ 1 ชิ้น Array (อาร์เรย์) คือกล่องที่เก็บของได้ หลายชิ้นเป็นชุด

let fruits = ["แอปเปิ้ล", "กล้วย", "ส้ม"];

ตัวอย่างการใช้งาน:

  • รายการสินค้าในตะกร้า
  • คะแนนสอบหลายวิชา
  • รายชื่อผู้ใช้

สร้าง Array

let numbers = [10, 20, 30];
let names = ["อร", "บอย", "เชียร์"];
let mixed = [1, "hello", true, 3.14];   // ผสมชนิดได้ (แต่ไม่แนะนำ)
let empty = [];                          // ว่าง

อ่านค่าด้วย index

index คือตำแหน่ง เริ่มนับที่ 0:

let fruits = ["แอปเปิ้ล", "กล้วย", "ส้ม"];
//              [0]        [1]     [2]

console.log(fruits[0]);   // แอปเปิ้ล
console.log(fruits[1]);   // กล้วย
console.log(fruits[2]);   // ส้ม

⚠️ index เริ่มที่ 0 (ไม่ใช่ 1) นี่คือเหตุผลว่าตัวสุดท้ายคือ length - 1

จำนวนสมาชิก: length

let fruits = ["แอปเปิ้ล", "กล้วย", "ส้ม"];
console.log(fruits.length);   // 3
console.log(fruits[fruits.length - 1]);   // ส้ม (ตัวสุดท้าย)

เปลี่ยนค่า

let fruits = ["แอปเปิ้ล", "กล้วย", "ส้ม"];
fruits[1] = "มะม่วง";
console.log(fruits);   // ["แอปเปิ้ล", "มะม่วง", "ส้ม"]

เพิ่ม/ลบสมาชิก

let fruits = ["แอปเปิ้ล", "กล้วย"];

fruits.push("ส้ม");          // เพิ่มท้าย → ["แอปเปิ้ล","กล้วย","ส้ม"]
fruits.unshift("ทุเรียน");    // เพิ่มหน้า → ["ทุเรียน","แอปเปิ้ล","กล้วย","ส้ม"]

let last = fruits.pop();     // เอาออกท้าย → ได้ "ส้ม"
let first = fruits.shift();  // เอาออกหน้า → ได้ "ทุเรียน"

วนลูปใน Array (ใช้บ่อยมาก!)

let scores = [80, 65, 90, 55];

// วิธีที่ 1: for...of
for (let score of scores) {
  console.log(score);
}

// วิธีที่ 2: forEach
scores.forEach((score) => {
  console.log(score);
});

เมธอด Array ที่ใช้บ่อย (ของยุคใหม่)

map — แปลงทุกตัว เป็น array ใหม่

let numbers = [1, 2, 3];
let doubled = numbers.map(n => n * 2);
console.log(doubled);   // [2, 4, 6]

filter — คัดเฉพาะที่ผ่านเงื่อนไข

let numbers = [1, 2, 3, 4, 5, 6];
let evens = numbers.filter(n => n % 2 === 0);
console.log(evens);   // [2, 4, 6]

find — หาตัวแรกที่ตรง

let users = ["อร", "บอย", "เชียร์"];
let found = users.find(u => u === "บอย");
console.log(found);   // บอย

includes — เช็คว่ามีค่านี้ไหม

let fruits = ["แอปเปิ้ล", "กล้วย"];
console.log(fruits.includes("กล้วย"));    // true
console.log(fruits.includes("มะม่วง"));   // false

join — รวมเป็นข้อความ

let words = ["Hello", "World"];
console.log(words.join(" "));   // "Hello World"

ลองทำจริง: คำนวณค่าเฉลี่ย

let scores = [80, 90, 75, 95];

let total = 0;
for (let score of scores) {
  total += score;
}
let average = total / scores.length;

console.log(`ค่าเฉลี่ย: ${average}`);   // 85

ลองทำจริง: ใช้ map + filter

let prices = [100, 250, 80, 500, 30];

// ราคาที่มากกว่า 100 แล้วลด 10%
let discounted = prices
  .filter(price => price > 100)
  .map(price => price * 0.9);

console.log(discounted);   // [225, 450]

สรุป

การกระทำเมธอด
เพิ่มท้ายpush()
เพิ่มหน้าunshift()
ลบท้ายpop()
ลบหน้าshift()
แปลงทุกตัวmap()
คัดเลือกfilter()
หาตัวแรกfind()
เช็คมีไหมincludes()

บทหน้า: มาเรียน Object — วิธีเก็บข้อมูลแบบมี "คุณสมบัติ" 🧱

อ่านจบแล้ว? ทำเครื่องหมายว่าเสร็จเพื่อบันทึกความคืบหน้า

🧠

แบบทดสอบ

ตอบให้ครบแล้วกด “ตรวจคำตอบ” เพื่อเช็คความเข้าใจ

  1. 1. ลำดับของ array เริ่มนับที่ index ใด?

  2. 2. คำสั่งใดใช้เพิ่มค่าต่อท้าย array?

  3. 3. ผลลัพธ์ของ `[1,2,3].length` คือ?

กำลังโหลด…