Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. รันฟังก์ชันซ้ำๆ ด้วย async/await ใน JavaScript

    ใช้ async/await ในลูป while เพื่อรอให้ฟังก์ชันแต่ละรอบทำงานเสร็จก่อนรอบถัดไป ตัวอย่างโค้ด async function test(i) { while (i <= 10) { await demo( Call Demo1()); await demo( Call Demo2()); i++; } } async function demo(value) { console.log(value); } test(1); วิธี

  2. สร้างตัวแปร Global ใน jQuery นอก document.ready

    ประกาศตัวแปรกลางแจ้งในแท็ก <script> ก่อน $(document).ready() ตัวอย่างโค้ด <!DOCTYPE html> <html lang=en> <head> <meta charset=UTF-8> <meta name=viewport content=width=device-width, initial-scale=1.0> <title>Document</title> <script

  3. แปลงสตริงให้ตัวแรกของแต่ละคำเป็นพิมพ์ใหญ่ ที่เหลือพิมพ์เล็กใน JavaScript

    ใช้ split(), map(), toUpperCase(), toLowerCase(), และ join() ตัวอย่างโค้ด st.charAt(0).toUpperCase() + st.slice(1).toLowerCase()) .join( ); } var subjectTitle = iNtroduction tO JavaScript ProgRAMMINg; var output = capitalizeEveryFirstLetter(subjectTitle); console.log(The result= + output)

  4. แปลง HH:MM:SS เป็นวินาทีใน JavaScript

    แยกชั่วโมง นาที วินาที แล้วคำนวณ: ชั่วโมง×3600 + นาที×60 + วินาที ตัวอย่างโค้ด var time = 10:8:20; var actualTime = time.split(:); console.log(The time= + time); var totalSeconds = (+actualTime[0]) * 60 * 60 + (+actualTime[1]) * 60 + (+actualTime[2]); console.log(Total Seconds= + totalSeconds); ว

  5. หาความยาวของ Associative Array ใน JavaScript

    ใช้ Object.keys() แล้วดู .length เพราะ Associative Array ใน JS เป็น Object ตัวอย่างโค้ด var details = new Array(); details[Name] = John; details[Age] = 21; details[CountryName] = US; details[SubjectName] = JavaScript; console.log(The length== + Object.keys(details).length); วิธีรันโปรแกรม ใช้คำสั

  6. แยกสตริงด้วยตัวคั่นหลังทุกคำใน JavaScript

    ใช้ split() แยกสตริงด้วยตัวคั่น แล้ว filter() กรองค่าว่างออก ตัวอย่างโค้ด value); console.log(After split()=); console.log(result); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo63.js) ผลลัพธ์ The value=-My-Name-is-John-Smith-I-live-in-US After split()= [ My, Name, is, John, Smith, I,

  7. วิธีรวมอาเรย์ของอ็อบเจ็กต์เป็นอ็อบเจ็กต์เดียวด้วย reduce()

    ใช้ reduce() รวมอาเรย์ของอ็อบเจ็กต์ให้กลายเป็นอ็อบเจ็กต์เดียว ตัวอย่างโค้ด { return { ...obj, ...value }; }, {}); console.log(objectStudent); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo64.js) ผลลัพธ์ { Name: Chris, Age: 22 }

  8. กรองอาเรย์ด้วย filter() และ includes() ใน JavaScript

    ใช้ filter() ร่วมกับ includes() เพื่อคัดกรองค่าที่ไม่อยู่ในรายการที่กำหนด ตัวอย่างโค้ด ![David, Mike, Sam, Carol].includes(allNameObject) ); console.log(After filter=); console.log(nameObject); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo65.js) ผลลัพธ์ The names= [ John, David

  9. ตรวจสอบว่าวันที่เป็นวันหยุดสุดสัปดาห์หรือไม่ใน JavaScript

    ใช้ getDay() คืนค่า 0 (อาทิตย์) ถึง 6 (เสาร์) ตรวจสอบว่าเป็น 0 หรือ 6 ตัวอย่างโค้ด var givenDate = new Date(2020-07-18); var currentDay = givenDate.getDay(); var dateIsInWeekend = (currentDay === 6) || (currentDay === 0); if (dateIsInWeekend == true) { console.log(The given date + givenDate +

  10. วิธีหาวินาทีนับจาก Epoch ใน JavaScript

    ใช้ Date.getTime() หาร 1000 แล้วปัดเศษด้วย Math.round() ไวยากรณ์ var anyVariableName = new Date(); Math.round(yourDateVariableName.getTime() / 1000); ตัวอย่างโค้ด var currentDate = new Date(); var epochSeconds = Math.round(currentDate.getTime() / 1000); console.log(epochSeconds); วิธีรันโปรแกรม ใ

  11. ลบสมาชิกออกจากอาเรย์ด้วย includes() และ splice() ใน JavaScript

    ใช้ includes() ตรวจสอบว่ามีค่าที่ต้องการลบหรือไม่ แล้วใช้ splice() ลบออก ตัวอย่างโค้ด function deleteElementsFromArray(elements, ...values) { let elementRemoved = Array.from(values); for (var index = 0; index < elements.length; index++) { if (elementRemoved.includes(elements[index])

  12. วิธีสร้างภาพเมทริกซ์พิกเซลในภาษา R

    แปลงเมทริกซ์เป็นภาพพิกเซลโดยใช้ฟังก์ชัน image() พร้อมอาร์กิวเมนต์ useRaster ตัวอย่างโค้ด > M <- matrix(rnorm(100, 1.5), nrow=10) > par(mar=c(5,5,5,5)) > image(M, useRaster=TRUE, axes=FALSE) ปรับขอบภาพด้วย par(mar=...) และใช้สีเทาด้วย col=grey(seq(0,1,length=180)) ผลลัพธ์ จะแสดงภาพเมทร

Total 5941 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:109/298  20-คอมพิวเตอร์/Page Goto:1 103 104 105 106 107 108 109 110 111 112 113 114 115