การใช้งาน method ต่าง ๆ ของ Array ใน JavaScript

เห็นมีคน share ภาพสรุป method ต่าง ๆ ของ Array ในภาษา JavaScript เลยลองไปดูหน่อยว่า แต่ละ method ทำงานและใช้อย่างไรบ้าง ซึ่งเป็นความรู้พื้นฐานของการพัฒนาระบบด้วยภาษา JavaScript มาเริ่มกันเลย

2021-10-18 15:53:16 - @ratanon

แต่ดูเหมือนว่าจากภาพ การใช้งาน fill() จะไม่ถูก

เพราะว่า fill() จะรับได้ 3 ค่า คือ

ดังนั้นต้องเปลี่ยนเป็น fill ( O , 1) ถึงจะถูกต้อง

ตัวอย่างของ code

const numbers = [1, 2, 3, 4, 5];
	
	console.log(numbers.map((x) => x * 2));
	console.log(numbers.filter((x) => x % 2 === 0));
	console.log(numbers.find((x) => x == 3));
	console.log(numbers.findIndex((x) => x == 3));
	console.log(numbers.some((x) => x == 2));
	console.log(numbers.every((x) => x == 100));
	
	console.log(numbers.fill(0, 1));
	
	===== ผลการทำงาน =====
	[ 2, 4, 6, 8, 10 ]
	[ 2, 4 ]
	3
	2
	true
	false
	[ 1, 0, 0, 0, 0 ]

ยังมี method อื่น ๆ อีกมากมายทั้ง

More Posts