Phương pháp Array.isArray[]
là một cách được khuyến nghị để kiểm tra xem một biến có phải là một mảng hay không vì nó có hỗ trợ trình duyệt tốt
Sau đây cho thấy một số ví dụ về việc sử dụng phương thức Array.isArray[]
const ratings = [1, 2, 3, 4, 5]; const vote = { user: 'John Doe', rating: 5 }; const str = "It isn't an array"; console.log[Array.isArray[ratings]]; // true console.log[Array.isArray[vote]]; // false console.log[Array.isArray[str]]; // false
Code language: JavaScript [javascript]
2] Sử dụng toán tử instanceof để kiểm tra xem một biến có phải là một mảng không
Vì tất cả các mảng đều là thể hiện của loại
2, nên bạn có thể sử dụng hàmCode language: JavaScript [javascript]
const ratings = [1, 2, 3, 4, 5]; const vote = { user: 'John Doe', rating: 5 }; const str = "It isn't an array"; console.log[Array.isArray[ratings]]; // true console.log[Array.isArray[vote]]; // false console.log[Array.isArray[str]]; // false
instanceof
để kiểm tra xem một biến có phải là một mảng như thế này không true
nếu value
là một Array
; . false
luôn được trả về nếu value
là một thể hiện của
// all following calls return true
Array.isArray[[]];
Array.isArray[[1]];
Array.isArray[new Array[]];
Array.isArray[new Array["a", "b", "c", "d"]];
Array.isArray[new Array[3]];
// Little known fact: Array.prototype itself is an array:
Array.isArray[Array.prototype];
// all following calls return false
Array.isArray[];
Array.isArray[{}];
Array.isArray[null];
Array.isArray[undefined];
Array.isArray[17];
Array.isArray["Array"];
Array.isArray[true];
Array.isArray[false];
Array.isArray[new Uint8Array[32]];
// This is not an array, because it was not created using the
// array literal syntax or the Array constructor
Array.isArray[{ __proto__: Array.prototype }];
2Array.isArray[]
kiểm tra xem giá trị đã truyền có phải là một Array
không. Nó không kiểm tra chuỗi nguyên mẫu của giá trị, cũng như không dựa vào hàm tạo Array
mà nó được gắn vào. Nó trả về true
cho bất kỳ giá trị nào được tạo bằng cách sử dụng cú pháp ký tự mảng hoặc hàm tạo Array
. Điều này làm cho nó an toàn khi sử dụng với các đối tượng liên lĩnh vực, trong đó danh tính của hàm tạo Array
là khác nhau và do đó sẽ khiến
// all following calls return true
Array.isArray[[]];
Array.isArray[[1]];
Array.isArray[new Array[]];
Array.isArray[new Array["a", "b", "c", "d"]];
Array.isArray[new Array[3]];
// Little known fact: Array.prototype itself is an array:
Array.isArray[Array.prototype];
// all following calls return false
Array.isArray[];
Array.isArray[{}];
Array.isArray[null];
Array.isArray[undefined];
Array.isArray[17];
Array.isArray["Array"];
Array.isArray[true];
Array.isArray[false];
Array.isArray[new Uint8Array[32]];
// This is not an array, because it was not created using the
// array literal syntax or the Array constructor
Array.isArray[{ __proto__: Array.prototype }];
9 thất bạiXem bài viết "Xác định với độ chính xác tuyệt đối xem một đối tượng JavaScript có phải là một mảng hay không" để biết thêm chi tiết
Array.isArray[]
cũng từ chối các đối tượng có
const iframe = document.createElement["iframe"];
document.body.appendChild[iframe];
const xArray = window.frames[window.frames.length - 1].Array;
const arr = new xArray[1, 2, 3]; // [1, 2, 3]
// Correctly checking for Array
Array.isArray[arr]; // true
// The prototype of arr is xArray.prototype, which is a
// different object from Array.prototype
arr instanceof Array; // false
1 trong chuỗi nguyên mẫu của nó nhưng không phải là mảng thực, mà // all following calls return true
Array.isArray[[]];
Array.isArray[[1]];
Array.isArray[new Array[]];
Array.isArray[new Array["a", "b", "c", "d"]];
Array.isArray[new Array[3]];
// Little known fact: Array.prototype itself is an array:
Array.isArray[Array.prototype];
// all following calls return false
Array.isArray[];
Array.isArray[{}];
Array.isArray[null];
Array.isArray[undefined];
Array.isArray[17];
Array.isArray["Array"];
Array.isArray[true];
Array.isArray[false];
Array.isArray[new Uint8Array[32]];
// This is not an array, because it was not created using the
// array literal syntax or the Array constructor
Array.isArray[{ __proto__: Array.prototype }];
9 sẽ chấp nhận// all following calls return true
Array.isArray[[]];
Array.isArray[[1]];
Array.isArray[new Array[]];
Array.isArray[new Array["a", "b", "c", "d"]];
Array.isArray[new Array[3]];
// Little known fact: Array.prototype itself is an array:
Array.isArray[Array.prototype];
// all following calls return false
Array.isArray[];
Array.isArray[{}];
Array.isArray[null];
Array.isArray[undefined];
Array.isArray[17];
Array.isArray["Array"];
Array.isArray[true];
Array.isArray[false];
Array.isArray[new Uint8Array[32]];
// This is not an array, because it was not created using the
// array literal syntax or the Array constructor
Array.isArray[{ __proto__: Array.prototype }];
Khi kiểm tra phiên bản Array
, Array.isArray[]
được ưu tiên hơn
const iframe = document.createElement["iframe"];
document.body.appendChild[iframe];
const xArray = window.frames[window.frames.length - 1].Array;
const arr = new xArray[1, 2, 3]; // [1, 2, 3]
// Correctly checking for Array
Array.isArray[arr]; // true
// The prototype of arr is xArray.prototype, which is a
// different object from Array.prototype
arr instanceof Array; // false
5 vì nó hoạt động trên các lĩnh vực