JS กวดวิชา

เจเอสโฮม JS Introduction JS Where To JS เอาท์พุต งบ JS JS ไวยากรณ์ JS ความคิดเห็น ตัวแปร JS เจเอส เลท JS Const JS Operators JS เลขคณิต JS Assignment ประเภทข้อมูล JS ฟังก์ชัน JS วัตถุ JS JS Events JS Strings JS String Methods JS String Search เทมเพลตสตริง JS JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates รูปแบบวันที่ JS JS วันที่รับเมธอด JS Date Set Methods JS Math JS สุ่ม JS Booleans JS เปรียบเทียบ JS Conditions JS Switch JS ลูปสำหรับ JS Loop สำหรับ In JS วนสำหรับ Of JS วนรอบในขณะที่ JS Break JS Iterables JS ชุด JS Maps JS Typeof JS ประเภทการแปลง JS Bitwise JS RegExp JS Errors JS ขอบเขต JS Hoisting JS โหมดเข้มงวด JS คำสำคัญนี้ JS Arrow Function JS Classes JS JSON JS Debugging คู่มือสไตล์ JS JS แนวทางปฏิบัติที่ดีที่สุด JS ความผิดพลาด JS Performance JS สงวนคำ

เวอร์ชัน JS

เวอร์ชัน JS JS 2009 (ES5) เจเอส 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / ขอบ JS History

วัตถุ JS

คำจำกัดความของวัตถุ คุณสมบัติของวัตถุ วิธีการวัตถุ การแสดงวัตถุ อุปกรณ์เสริมวัตถุ ตัวสร้างวัตถุ ต้นแบบวัตถุ วัตถุ Iterables ชุดวัตถุ แผนที่วัตถุ การอ้างอิงวัตถุ

ฟังก์ชัน JS

คำจำกัดความของฟังก์ชัน พารามิเตอร์ฟังก์ชัน เรียกใช้ฟังก์ชัน เรียกฟังก์ชัน ฟังก์ชัน สมัคร การปิดฟังก์ชัน

JS Classes

แนะนำคลาส การสืบทอดคลาส คลาสคงที่

JS Async

JS Callbacks JS แบบอะซิงโครนัส JS Promises JS Async/รอ

JS HTML DOM

บทนำ DOM วิธี DOM เอกสาร DOM องค์ประกอบ DOM DOM HTML แบบฟอร์ม DOM DOM CSS ภาพเคลื่อนไหว DOM กิจกรรม DOM DOM Event Listener การนำทาง DOM โหนด DOM DOM Collections รายการโหนด DOM

JS เบราว์เซอร์ BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

แนะนำ Web API เว็บฟอร์ม API API ประวัติเว็บ API ที่เก็บข้อมูลเว็บ Web Worker API API ดึงเว็บ เว็บ Geolocation API

JS AJAX

บทนำ AJAX AJAX XMLHttp คำขอ AJAX การตอบสนอง AJAX ไฟล์ AJAX XML AJAX PHP AJAX ASP ฐานข้อมูล AJAX แอปพลิเคชัน AJAX ตัวอย่าง AJAX

JS JSON

บทนำ JSON ไวยากรณ์ JSON JSON กับ XML ประเภทข้อมูล JSON JSON Parse JSON สตริงริฟาย วัตถุ JSON อาร์เรย์ JSON เซิร์ฟเวอร์ JSON JSON PHP JSON HTML JSON JSONP

JS กับ jQuery

ตัวเลือก jQuery jQuery HTML jQuery CSS jQuery DOM

JS กราฟิก

JS กราฟิก JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

ตัวอย่าง JS

ตัวอย่าง JS JS HTML DOM JS HTML อินพุต JS HTML Objects JS HTML Events JS Browser JS Editor JS แบบฝึกหัด JS Quiz ใบรับรอง JS

JS References

วัตถุ JavaScript วัตถุ HTML DOM


JavaScript Array Iteration


วิธีการวนซ้ำของอาร์เรย์ทำงานบนทุกรายการอาร์เรย์


JavaScript Array forEach()

เมธอดเรียกใช้ ฟังก์ชันforEach()(ฟังก์ชันเรียกกลับ) หนึ่งครั้งสำหรับแต่ละองค์ประกอบอาร์เรย์

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);

function myFunction(value, index, array) {
  txt += value + "<br>";
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

ตัวอย่างข้างต้นใช้เฉพาะค่าพารามิเตอร์ ตัวอย่างสามารถเขียนใหม่เป็น:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);

function myFunction(value) {
  txt += value + "<br>";
}

แผนที่อาร์เรย์ JavaScript ()

วิธี การmap()สร้างอาร์เรย์ใหม่โดยดำเนินการกับแต่ละองค์ประกอบอาร์เรย์

เมธอด นี้map()จะไม่เรียกใช้ฟังก์ชันสำหรับองค์ประกอบอาร์เรย์ที่ไม่มีค่า

วิธี การmap()นี้จะไม่เปลี่ยนอาร์เรย์เดิม

ตัวอย่างนี้คูณค่าอาร์เรย์แต่ละค่าด้วย 2:

ตัวอย่าง

const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);

function myFunction(value, index, array) {
  return value * 2;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

เมื่อฟังก์ชันเรียกกลับใช้เฉพาะพารามิเตอร์ค่า พารามิเตอร์ดัชนีและอาร์เรย์สามารถละเว้นได้:

ตัวอย่าง

const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);

function myFunction(value) {
  return value * 2;
}


ตัวกรองอาร์เรย์ JavaScript ()

วิธี การfilter()สร้างอาร์เรย์ใหม่ที่มีองค์ประกอบอาร์เรย์ที่ผ่านการทดสอบ

ตัวอย่างนี้สร้างอาร์เรย์ใหม่จากองค์ประกอบที่มีค่ามากกว่า 18:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

ในตัวอย่างข้างต้น ฟังก์ชันเรียกกลับไม่ได้ใช้พารามิเตอร์ดัชนีและอาร์เรย์ จึงสามารถละเว้นได้:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);

function myFunction(value) {
  return value > 18;
}

JavaScript Array ลด ()

เมธอดรัน ฟังก์ชันreduce()บนแต่ละอิลิเมนต์อาร์เรย์เพื่อสร้าง (ลดให้เหลือ) ค่าเดียว

วิธี การreduce()ทำงานจากซ้ายไปขวาในอาร์เรย์ ดูเพิ่มเติมreduceRight()ที่

วิธี การreduce()ไม่ลดอาร์เรย์ดั้งเดิม

ตัวอย่างนี้ค้นหาผลรวมของตัวเลขทั้งหมดในอาร์เรย์:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction);

function myFunction(total, value, index, array) {
  return total + value;
}

โปรดทราบว่าฟังก์ชันรับ 4 อาร์กิวเมนต์:

  • ยอดรวม (ค่าเริ่มต้น / มูลค่าที่ส่งคืนก่อนหน้านี้)
  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

ตัวอย่างข้างต้นไม่ได้ใช้พารามิเตอร์ดัชนีและอาร์เรย์ สามารถเขียนใหม่เป็น:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction);

function myFunction(total, value) {
  return total + value;
}

วิธี การreduce()นี้สามารถรับค่าเริ่มต้นได้:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction, 100);

function myFunction(total, value) {
  return total + value;
}

JavaScript Array ลดด้านขวา ()

เมธอดรัน ฟังก์ชันreduceRight()บนแต่ละอิลิเมนต์อาร์เรย์เพื่อสร้าง (ลดให้เหลือ) ค่าเดียว

ทำงาน จากreduceRight()ขวาไปซ้ายในอาร์เรย์ ดูเพิ่มเติมreduce()ที่

วิธี การreduceRight()ไม่ลดอาร์เรย์ดั้งเดิม

ตัวอย่างนี้ค้นหาผลรวมของตัวเลขทั้งหมดในอาร์เรย์:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let sum = numbers1.reduceRight(myFunction);

function myFunction(total, value, index, array) {
  return total + value;
}

โปรดทราบว่าฟังก์ชันรับ 4 อาร์กิวเมนต์:

  • ยอดรวม (ค่าเริ่มต้น / มูลค่าที่ส่งคืนก่อนหน้านี้)
  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

ตัวอย่างข้างต้นไม่ได้ใช้พารามิเตอร์ดัชนีและอาร์เรย์ สามารถเขียนใหม่เป็น:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let sum = numbers1.reduceRight(myFunction);

function myFunction(total, value) {
  return total + value;
}

JavaScript Array ทุก ()

วิธี การevery()ตรวจสอบว่าค่าอาร์เรย์ทั้งหมดผ่านการทดสอบหรือไม่

ตัวอย่างนี้ตรวจสอบว่าค่าอาร์เรย์ทั้งหมดมากกว่า 18:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

เมื่อฟังก์ชันเรียกกลับใช้พารามิเตอร์แรกเท่านั้น (ค่า) พารามิเตอร์อื่นสามารถละเว้นได้:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);

function myFunction(value) {
  return value > 18;
}

JavaScript Array บาง ()

วิธี การsome()ตรวจสอบว่าค่าอาร์เรย์บางค่าผ่านการทดสอบหรือไม่

ตัวอย่างนี้ตรวจสอบว่าค่าอาร์เรย์บางค่ามากกว่า 18:

ตัวอย่าง

const numbers = [45, 4, 9, 16, 25];
let someOver18 = numbers.some(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

JavaScript Array indexOf()

วิธี การindexOf()ค้นหาค่าองค์ประกอบในอาร์เรย์และส่งกลับตำแหน่ง

หมายเหตุ:รายการแรกมีตำแหน่ง 0 รายการที่สองมีตำแหน่ง 1 และอื่นๆ

ตัวอย่าง

ค้นหาอาร์เรย์สำหรับรายการ "Apple":

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;

ไวยากรณ์

array.indexOf(item, start)
item Required. The item to search for.
start Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end.

Array.indexOf()ส่งคืน -1 หากไม่พบรายการ

หากรายการมีอยู่มากกว่าหนึ่งครั้ง รายการนั้นจะส่งกลับตำแหน่งที่เกิดครั้งแรก


อาร์เรย์ JavaScript LastIndexOf()

Array.lastIndexOf()เหมือนกับArray.indexOf()แต่ส่งคืนตำแหน่งของการเกิดขึ้นครั้งสุดท้ายขององค์ประกอบที่ระบุ

ตัวอย่าง

ค้นหาอาร์เรย์สำหรับรายการ "Apple":

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;

ไวยากรณ์

array.lastIndexOf(item, start)
item Required. The item to search for
start Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the beginning

ค้นหาอาร์เรย์ JavaScript ()

วิธี การfind()ส่งคืนค่าขององค์ประกอบอาร์เรย์แรกที่ผ่านฟังก์ชันการทดสอบ

ตัวอย่างนี้จะค้นหา (คืนค่าของ) องค์ประกอบแรกที่มากกว่า 18:

ตัวอย่าง

const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

รองรับเบราว์เซอร์

find() เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

find() ไม่รองรับใน Internet Explorer


อาร์เรย์ JavaScript findIndex()

เมธอดจะคืน ค่าfindIndex()ดัชนีขององค์ประกอบอาร์เรย์แรกที่ผ่านการทดสอบฟังก์ชัน

ตัวอย่างนี้ค้นหาดัชนีขององค์ประกอบแรกที่มากกว่า 18:

ตัวอย่าง

const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

โปรดทราบว่าฟังก์ชันรับ 3 อาร์กิวเมนต์:

  • มูลค่ารายการ
  • ดัชนีรายการ
  • อาร์เรย์ตัวเอง

รองรับเบราว์เซอร์

findIndex() เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

findIndex() ไม่รองรับใน Internet Explorer




JavaScript Array.from()

เมธอดส่ง คืนArray.from()อ็อบเจ็กต์ Array จากอ็อบเจ็กต์ใดๆ ที่มีคุณสมบัติ length หรืออ็อบเจกต์ iterable ใดๆ

ตัวอย่าง

สร้างอาร์เรย์จากสตริง:

Array.from("ABCDEFG");

รองรับเบราว์เซอร์

from() เป็นคุณลักษณะ ES6 (JavaScript 2015)

ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

from() ไม่รองรับใน Internet Explorer


คีย์อาร์เรย์ JavaScript ()

เมธอดส่ง คืนArray.keys()อ็อบเจ็กต์ Array Iterator ด้วยคีย์ของอาร์เรย์

ตัวอย่าง

Create an Array Iterator object, containing the keys of the array:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
const keys = fruits.keys();

for (let x of keys) {
  text += x + "<br>";
}

Browser Support

keys() is an ES6 feature (JavaScript 2015).

It is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

keys() is not supported in Internet Explorer.


JavaScript Array includes()

ECMAScript 2016 introduced Array.includes() to arrays. This allows us to check if an element is present in an array (including NaN, unlike indexOf).

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.includes("Mango"); // is true

Syntax

array.includes(search-item)

Array.includes() allows to check for NaN values. Unlike Array.indexOf().

Array.includes() is not supported in Internet Explorer and Edge 12/13.

The first browser versions with full support are:


Browser Support

includes() is an ECMAScript 2016 feature.

It is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

includes() is not supported in Internet Explorer.


Complete Array Reference

For a complete Array reference, go to our:

Complete JavaScript Array Reference.

The reference contains descriptions and examples of all Array properties and methods.