Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript
JavaScript
  1. JavaScript มี Null Coalescing Operator หรือไม่

    JavaScript รองรับ Nullish Coalescing Operator (??) และเดิมๆ ใช้ Logical OR (||) ตัวอย่างโค้ด (ใช้ ||) var fullName = null; console.log(The full name is= + fullName); var actualName = fullName || David Miller; console.log(The full name is= + actualName); ตัวอย่างโค้ด (ใช้ ?? - ES2020) var fullName

  2. สร้างอาเรย์จากข้อมูล JSON ด้วย map() ใน JavaScript

    ใช้ map() ดึง Property ที่ต้องการจากอาเรย์อ็อบเจ็กต์ สร้างอาเรย์ใหม่ ตัวอย่างโค้ด actualValue); console.log(listOfStudentName); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo82.js) ผลลัพธ์ [ John, David, Bob ]

  3. กรองอาเรย์อ็อบเจ็กต์ตาม Property เฉพาะใน JavaScript

    ใช้ map() ร่วมกับ Ternary Operator เลือก Object ที่มีค่า Property มากกว่า ตัวอย่างโค้ด secondCustomerDetails[position].amount ? key : secondCustomerDetails[position] ); console.log(output); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo83.js) ผลลัพธ์ [ { firstName: John, amount: 40

  4. หา Object ที่มีค่ามากที่สุดจาก 2 อาเรย์โดยไม่ต้อง Concat ใน JavaScript

    ใช้ reduce() บนแต่ละอาเรย์แยกกัน เก็บค่าที่มากกว่าตาม Key ตัวอย่างโค้ด = previousValue.studentMarks) { arrayValues[k.studentName] = k; } return arrayValues; } const sectionA = sectionAStudentDetails.reduce(mergeByMaxMarks, {}); const sectionB = sectionBStudentDetails.reduce(mergeBy

  5. เขียน For Loop รวมค่าทุกตัวในอาเรย์เก็บในตัวแปร JavaScript

    ประกาศตัวแปร total = 0 แล้ววนลูปบวกค่าทุกตัวในอาเรย์ ตัวอย่างโค้ด var listOfValues = [10, 3, 4, 90, 34, 56, 23, 100, 200]; var total = 0; for (let index = 0; index < listOfValues.length; index++) { total = total + listOfValues[index]; } console.log(Total Values= + total); วิธีรันโปรแกรม ใช้ค

  6. แก้ไข Key ของ Object ลบ Underscore และเปลี่ยนเป็น CamelCase

    ใช้ Regex กับ Object.fromEntries() และ Object.entries() แปลง Key ตัวอย่างโค้ด [underscoreToCamelCase(key), value]) ); console.log(The JSON Object=); console.log(output); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo89.js) ผลลัพธ์ The JSON Object= { firstNameField: John, lastNameFiel

  7. ลบศูนย์นำหน้า (Leading Zeros) ในอาเรย์ JavaScript

    ใช้ filter() กรองค่า 0 นำหน้าออก (แต่เก็บ 0 ที่อยู่กลาง/ท้ายไว้) ตัวอย่างโค้ด lastValue = lastValue || value)(false) ); console.log(removeLeadingZero([10, 0, 12, 0, 0])); console.log(removeLeadingZero([0, 0, 0, 0, 0, 0, 10, 12, 0])); console.log(removeLeadingZero([12, 0, 0, 1, 0, 0])); วิธีรันโป

  8. เปลี่ยน Property ของ Object แบบมีเงื่อนไขใน JavaScript

    ใช้ Logical AND (&&) ร่วมกับ Spread Operator เพื่อเพิ่ม Property ก็ต่อเมื่อเงื่อนไขเป็นจริง ตัวอย่างโค้ด let marksDetails = { Mark1: 33, Mark2: 89 }; let isBacklog = false; console.log(Result when backlog is set to false===); console.log({ ...marksDetails, ...(isBacklog === true && { Mark1: 33 })

  9. รวมค่า Map เป็นสตริงเดียวด้วย join() ใน JavaScript

    ใช้ Map เก็บค่า แล้ว Array.from(), map(), join() รวมเป็นสตริง ตัวอย่างโค้ด value.join(?)) .join(?); console.log(The appended value is= + appendValue); วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo78.js) ผลลัพธ์ The appended value is=name=John?age=23?countryName=US?subjectName=Ja

  10. ดึงค่าเฉพาะจากอาเรย์ด้วย eval() ใน JavaScript

    eval() ประเมินสตริงเป็นโค้ด JavaScript ใช้เข้าถึงตัวแปรจากชื่อสตริง ตัวอย่างโค้ด var studentMarks = [76, 34, 56, 78, 81, 98, 90, 59, 64]; var stringValue = eval(studentMarks); console.log(stringValue[1]); // ดึงค่าดัชนี 1 วิธีรันโปรแกรม ใช้คำสั่ง node fileName.js (เช่น node demo80.js) ผลลัพธ์ 34

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