Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

การอัปเดตดัชนีเฉพาะในเมทริกซ์บูลลีนด้วย JavaScript

ใช้เมธอด fill() เพื่อสร้างเมทริกซ์บูลลีนและอัปเดตค่า

ตัวอย่างโค้ด

const array = Array(4);
var fillWithTrueValue = array.fill(true);
const matrixWithOnlyBooleanTrue = Array(4).fill(fillWithTrueValue);
console.log(matrixWithOnlyBooleanTrue);

วิธีรันโปรแกรม

ใช้คำสั่ง node fileName.js (เช่น node demo59.js)

ผลลัพธ์

[
  [ true, true, true, true ],
  [ true, true, true, true ],
  [ true, true, true, true ],
  [ true, true, true, true ]
]

หมายเหตุ: วิธีนี้จะสร้างอ้างอิงไปยังอาเรย์เดียวกัน ทุกแถวจะชี้ไปที่อาเรย์ตัวเดียวกัน การเปลี่ยนค่าที่หนึ่งจะส่งผลต่อทุกแถว