Basic Javascript - Array
Array คือ ตัวแปรอีกชนิดหนึ่ง สร้างมาไว้สำหรับเก็บข้อมูลไว้ข้างในหลายๆตัวอีกที่
การประการและเรียกใช้งาน Array แบบปกติ
test("Access array element by index", function(){
let numbers = [ 1, 2, 3, 4, 5 ]
expect(numbers[0]).toEqual(1)
expect(numbers[1]).toEqual(2)
expect(numbers[2]).toEqual(3)
expect(numbers[3]).toEqual(4)
expect(numbers[4]).toEqual(5)
})
การวนลูปเพื่อเรียกใช้งานค่าต่างๆที่อยู่ใน Array
แบบปกติ
test("Traditional for loop through array", function() {
let a = [1,2,3]
let b = [1,2,3]
for(let i = 0; i < a.length; i++){
expect(a[i]).toEqual(b[i])
}
})
แบบใช้ฟังกชั่น Each
test("For loop through array using each function", function() {
let a = [1,2,3]
a.each(function(element, index) {
expect(element).toEqual(a[index])
})
})
สามารถนำไปประยุกต์ใช้สำหรับสร้าง Data driven test ได้