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ình | Một boolean | true Nếu giá trị được tìm thấy, nếu không false . |
Hỗ trợ trình duyệt | Hỗ trợ trình duyệt | Hỗ trợ trình duyệt | Hỗ trợ trình duyệt | Hỗ trợ trình duyệt |
includes[]
là tính năng ECMAScript7 [ES7].
Trong JavaScript, mảng là một biến duy nhất được sử dụng để lưu trữ các yếu tố khác nhau. Nó thường được sử dụng khi chúng tôi muốn lưu trữ một danh sách các yếu tố và truy cập chúng bằng một biến duy nhất. Không giống như hầu hết các ngôn ngữ trong đó mảng là tham chiếu đến nhiều biến, trong mảng JavaScript là một biến duy nhất lưu trữ nhiều phần tử.
Tuyên bố của một mảng: Về cơ bản có hai cách để khai báo một mảng. There are basically two ways to declare an array.
Example:
var House = [ ]; // Method 1 var House = new Array[]; // Method 2
Lưu ý: Nhưng nói chung Phương pháp 1 được ưu tiên hơn phương pháp 2. Hãy cho chúng tôi hiểu lý do cho điều này. But generally method 1 is preferred over method 2. Let us understand the reason for this.
Ví dụ: Khởi tạo một mảng theo phương pháp 1. Initialization of an Array according to method 1.
JavaScript
Is
Ví dụ: Khởi tạo một mảng theo phương pháp 2. Initialization of an Array according to method 2.
JavaScript
true
0
Is
Ví dụ: Khởi tạo một mảng theo phương pháp 2.
true
1includes[]
0 true
3true
4 true
5
includes[]
8
true
1includes[]
0 true
8true
4 includes[]
0house contains 5 elements i.e. [10 , 20, 30, 40, 50] while house1 contains 5 undefined elements instead of having a single element 5. Hence, while working with numbers this method
is generally not preferred but it works fine with Strings and Boolean as shown in the example above home contains a single element 1BHK.
true
1includes[]
0 includes[]
3true
4 includes[]
5includes[]
2includes[]
7 We can also update after initialization.
JavaScript
true
0
Is
true
1false
6includes[]
2
true
1false
9includes[]
4
true
1includes[]
2includes[]
6
Ví dụ: Khởi tạo một mảng theo phương pháp 2.
includes[]
8
true
1includes[]
0 true
3true
4 true
5 An array in JavaScript can hold different elements that can store Numbers, Strings, and Boolean in a single array.
JavaScript
true
0
Is
includes[]
8
Ví dụ: Khởi tạo một mảng theo phương pháp 2. Accessing Array Elements of an Array in JavaScript are indexed from 0 so we can access array elements as follows.
JavaScript
true
0
Is
true
1includes[]
4includes[]
5includes[]
6
Ví dụ: Khởi tạo một mảng theo phương pháp 2.
true
1includes[]
0 true
3true
4 true
5
true
1includes[]
4includes[]
5includes[]
6
true
1includes[]
4includes[]
9includes[]
00
includes[]
8
true
1includes[]
0 true
8true
4 includes[]
0 Length property of an Array Length property of an Array returns the length of an
Array. The length of an Array is always one more than the highest index of an Array.
JavaScript
true
0
Is
Ví dụ: Khởi tạo một mảng theo phương pháp 2.
true
1includes[]
0 true
3true
4 true
5
includes[]
22includes[]
23
includes[]
8
true
1includes[]
0 true
8true
4 includes[]
0 All the above examples can be tested by typing them within the script tag of HTML
For
commonly used Array methods refer to the links below.
true
1includes[]
0includes[]
3true
4includes[]
5includes[]
2includes[]
7- Như thể hiện trong ví dụ ở trên, ngôi nhà chứa 5 phần tử, tức là [10, 20, 30, 40, 50] trong khi House1 chứa 5 phần tử không xác định thay vì có một phần tử 5. Do đó, trong khi làm việc với các số, phương pháp này thường không được ưa thích nhưng nó Hoạt động tốt với các chuỗi và boolean như trong ví dụ trên nhà chứa một phần tử 1BHK.
- Ví dụ: Chúng tôi cũng có thể cập nhật sau khi khởi tạo.
true
1includes[]
0 true
8true
4 false
4Sumit
Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
true
1includes[]
55includes[]
8