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
เรื่องอื่น ๆ ที่เกี่ยวข้อง

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

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

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

การติดตั้งและใช้งาน highchart ร่วมกับ angular
เมื่อวันที่: 22 ก.พ. 2565 18:31 น.

พัฒนา Mobile Application ด้วย React Native Expo เขียนครั้งเดียวใช้ได้ทั้ง Android และ iOS
เมื่อวันที่: 15 เม.ย. 2565 17:12 น.

วิธีติดตั้ง Node.js บน Macbook (macOS)
เมื่อวันที่: 13 ก.พ. 2565 23:13 น.