Javascript codes ที่ใช้บ่อย ๆ
วันที่: 27 มี.ค. 2565 23:55 น.

การเรียงลำดับ Array (array sort)
const no = [4, 2, 1, 3];
no.sort();
// [1, 2, 3, 4]
การเรียงลำดับแบบสุ่มของ Array
const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random());
console.log(shuffleArray([1, 2, 3, 4, 5]));
// [1, 5, 4, 2, 3]
การเรียงลำดับกรณีมี object key value
const myArray = [{name:"alex"},{name:"clex"},{name:"blex"}];
myArray.sort((a,b)=> (a.name > b.name ? 1 : -1)); // ASC
// myArray.sort((a,b)=> (a.name < b.name ? 1 : -1)); // DESC
console.log(myArray);
การเช็คว่ามีค่าที่ต้องการใน Array หรือไม่
var myArray = [2,5,6,7,9,6];
console.log(myArray.includes(2)) // is true
console.log(myArray.includes(14)) // is false
การตัดค่าซ้ำของ Array (remove duplicates)
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
การหาจำนวนวันระหว่างวันที่
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2021-10-21"), new Date("2022-10-22"))
// Result: 366
เรื่องอื่น ๆ ที่เกี่ยวข้อง

การเรียงข้อมูลใน array โดยเรียงจาก ข้อมูลใน object
เมื่อวันที่: 30 ส.ค. 2565 19:50 น.

แจกโค้ด CSS Hover Effect ที่ใช้บ่อย ๆ
เมื่อวันที่: 7 มิ.ย. 2566 09:49 น.

สร้าง RESTful API เบื้องต้นสำหรับ เพิ่ม/ลบ/แก้ไข ด้วย Node.js ร่วมกับ Express
เมื่อวันที่: 7 เม.ย. 2565 23:00 น.

เริ่มต้นสร้าง RESTful API ด้วย Node.js ร่วมกับ Express
เมื่อวันที่: 6 เม.ย. 2565 22:57 น.

Json Web Token (JWT) brute force การเดารหัสหรือ secret key ด้วย node.js
เมื่อวันที่: 14 ก.ย. 2565 21:03 น.

เมื่อวันที่: 16 ก.พ. 2565 16:13 น.