Hướng dẫn how do you check if the element is an array in an array javascript? - làm cách nào để kiểm tra xem phần tử có phải là một mảng trong một mảng javascript không?

Ví dụ

const fruits = ["chuối", "cam", "táo", "xoài"]; quả.includes ("xoài");
fruits.includes("Mango");

Hãy tự mình thử »

Bắt đầu tìm kiếm ở vị trí 3:

const fruits = ["chuối", "cam", "táo", "xoài"]; trái cây.includes ("chuối", 3);
fruits.includes("Banana", 3);

Hãy tự mình thử »


Bắt đầu tìm kiếm ở vị trí 3:

const fruits = ["chuối", "cam", "táo", "xoài"]; trái cây.includes ("chuối", 3);

Định nghĩa và cách sử dụng

Phương thức includes() trả về true nếu một mảng chứa một giá trị được chỉ định.


Phương thức includes() trả về false nếu không tìm thấy giá trị.

Phương pháp includes() là trường hợp nhạy cảm.

Cú pháp

mảng.includes (phần tử, bắt đầu)Thông số
Tham sốSự mô tả
The value to search for.
yếu tốYêu cầu. Giá trị để tìm kiếm.
Start position. Default is 0.

bắt đầu

Tùy chọn. Vị trí khởi động. Mặc định là 0.Thông số
Tham sốSự mô tả

yếu tố

Yêu cầu. Giá trị để tìm kiếm.

bắt đầu

Tùy chọn. Vị trí khởi động. Mặc định là 0.Giá trị trả vềLoại hìnhMột boolean true nếu tìm thấy giá trị, nếu không false.
Hỗ trợ trình duyệtHỗ trợ trình duyệtHỗ trợ trình duyệtHỗ trợ trình duyệtHỗ trợ trình duyệt

includes() là tính năng ECMAScript7 (ES7).



Phương pháp tĩnh

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 xác định xem giá trị truyền có phải là
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
2 hay không.
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
static method determines whether the passed value is an
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
2.

Cú pháp

Thông số

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
3

Giá trị cần kiểm tra.

Giá trị trả về

true nếu

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
3 là
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
2; Nếu không, false. false luôn được trả lại nếu
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
3 là một ví dụ includes()0.

Sự mô tả

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 Kiểm tra xem giá trị truyền có phải là
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
2 không. Nó không kiểm tra chuỗi nguyên mẫu của giá trị, cũng không dựa vào hàm tạo
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
2 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ú pháp theo nghĩa đen hoặc hàm tạo
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
2. Điều này làm cho việc sử dụng an toàn với các đối tượng chéo realm, trong đó danh tính của hàm tạo
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
2 là khác nhau và do đó sẽ khiến includes()7 bị lỗi.

Xem bài viết "Xác định độ chính xác tuyệt đối cho dù đối tượng JavaScript có phải là một mảng" để biết thêm chi tiết hay không.

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 cũng từ chối các đối tượng có includes()9 trong chuỗi nguyên mẫu của nó nhưng không phải là mảng thực tế, mà includes()7 sẽ chấp nhận.

Ví dụ

Sử dụng Array.isarray ()

// 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 });

InstanceOf vs Array.isarray ()

Khi kiểm tra ví dụ

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
2,
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 được ưa thích hơn true3 vì nó hoạt động trên các vương quố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

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-marray.isarray
# sec-array.isarray

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm