Trong JavaScript, có nhiều cách để kiểm tra xem một mảng có bao gồm một mục hay không. Ngoài các vòng lặp, bạn có thể sử dụng
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7, fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8, fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9, v.v. để kiểm tra xem giá trị hoặc phần tử đã cho có tồn tại trong một mảng hay khôngPhương pháp fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Phương thức
const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
1 đã được thêm vào ES6 để xác định xem một mảng có chứa một giá trị được chỉ định hay không. Phương thức này trả về const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
2 nếu phần tử tồn tại trong mảng và const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
3 nếu khôngPhương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 hoàn hảo để tìm xem phần tử có tồn tại hay không dưới dạng giá trị boolean đơn giản________số 8Theo mặc định, phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 tìm kiếm toàn bộ mảng. Nhưng bạn cũng có thể chuyển vào chỉ mục bắt đầu dưới dạng tham số thứ hai để bắt đầu tìm kiếm từ một vị trí khácfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Bên cạnh chuỗi, phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 cũng hoạt động tốt với các kiểu nguyên thủy khácconst symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
Cả
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 và fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8 đều hoạt động khác nhau với thuộc tính const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
9 ["Không phải là số"]fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
6Phương pháp
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 không hoạt động trong IE và chỉ khả dụng trong các trình duyệt hiện đạiPhương pháp fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Cách đơn giản nhất và nhanh nhất để kiểm tra xem một phần tử có trong một mảng hay không là sử dụng phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
62. Phương thức này tìm kiếm mảng cho giá trị đã cho và trả về chỉ mục của nó. Nếu không tìm thấy mục nào, nó sẽ trả về -1fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
0Theo mặc định, phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8 bắt đầu tìm kiếm mục đã cho từ đầu mảng và dừng ở cuối mảng. Nhưng bạn có thể chuyển vào một vị trí làm đối số thứ hai để bỏ qua các phần tử bắt đầu được đưa vào tìm kiếmfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
2Lưu ý rằng nếu mục xuất hiện nhiều lần, phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8 trả về vị trí của lần xuất hiện đầu tiênJavaScript cung cấp cho chúng ta một phương thức mảng thay thế có tên là
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
65. Như tên gợi ý, nó trả về vị trí xuất hiện cuối cùng của các mục trong một mảngfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
65 bắt đầu tìm kiếm mảng từ cuối và dừng ở đầu mảng. Bạn cũng có thể chỉ định tham số thứ hai để loại trừ các mục ở cuốifruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
6Cả
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8 và fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
65 đều thực hiện tìm kiếm phân biệt chữ hoa chữ thường và hoạt động trên tất cả các trình duyệt, bao gồm IE9 trở lênPhương pháp fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Không giống như
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7, phương thức fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9 thực thi hàm được chỉ định cho từng phần tử trong mảng. Nó trả về giá trị của phần tử đầu tiên trong mảng thỏa mãn điều kiện đã chofruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
2Ghi chú. Trong ví dụ trên, tôi đã sử dụng chức năng mũi tên ES6 để lặp qua các phần tử. Đọc bài viết này để tìm hiểu thêm về chức năng mũi tên
Nếu không tìm thấy phần tử nào trong đó hàm trả về
const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
2, phương thức fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9 trả về giá trị fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
04fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
6Bạn cũng có thể lấy chỉ mục phần tử hiện tại làm tham số thứ hai. Điều này cũng hữu ích khi bạn muốn so sánh chỉ số
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7Một lợi ích khác của phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9 là nó cũng hoạt động với các loại dữ liệu khác như đối tượngfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
0Phương pháp
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9 chỉ hoạt động trong các trình duyệt hiện đạiPhương pháp fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07 tương tự như phương thức fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
9 ngoại trừ việc nó trả về giá trị boolean const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
2 nếu phần tử được tìm thấy trong mảng, nếu không thì const symbol = Symbol['🌟']
const types = ['Apple', 150, null, undefined, true, 29n, symbol]
// strings
types.includes['Apple'] // true
// numbers
types.includes[150] // true
// null
types.includes[null] // true
// undefined
types.includes[undefined] // true
// boolean
types.includes[true] // true
// BigInt
types.includes[29n] // true
// Symbol
types.includes[symbol // true
3fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
1Phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07 cũng có thể được sử dụng với một mảng các đối tượngfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
2Bạn có thể sử dụng phương pháp
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07 trong tất cả các trình duyệt hiện đại và trong IE9 trở lênPhương pháp fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
24
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
Phương thức
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
24 giống như fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07 ngoại trừ việc nó đảm bảo rằng tất cả các phần tử trong mảng đều vượt qua một điều kiện nhất địnhfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
3Cũng giống như
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07, fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
24 hoạt động trên tất cả các trình duyệt hiện đại và IE9 trở lênTìm kiếm không phân biệt chữ hoa chữ thường
Cả hai phương pháp
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
8 và fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
7 đều phân biệt chữ hoa chữ thường. Điều này có nghĩa là bạn phải chỉ định cùng một chuỗi trường hợp để tìm kiếm mảngfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
4Đối với tìm kiếm không phân biệt chữ hoa chữ thường, một cách là chuyển đổi từng chuỗi trong mảng thành chữ thường bằng cách sử dụng phương thức map[] rồi thực hiện tìm kiếm
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
5Ngoài ra, bạn có thể sử dụng phương pháp
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
07 để thực hiện cả chữ thường và so sánh chuỗi trong một bướcfruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
6Phần kết luận
Trong bài viết này, chúng ta đã xem xét 5 phương thức khác nhau của JavaScript
fruits.includes['🍐', 4] // true
fruits.includes['🍊', 4] // false
62 để kiểm tra xem một mục có tồn tại trong một mảng hay khôngMột câu hỏi chính đáng là tại sao chúng ta cần tất cả các phương pháp này ngay từ đầu?
Một câu trả lời đơn giản là tất cả các phương pháp này đều dành cho các trường hợp sử dụng khác nhau
- Muốn biết vị trí phần tử trong mảng?
- Bạn muốn tìm vị trí xuất hiện cuối cùng của một phần tử?
- Bạn chỉ muốn biết liệu phần tử có tồn tại hay không?
- Bạn có muốn lấy phần tử phù hợp không?
- Làm việc với một mảng các đối tượng?
- Bạn muốn thực hiện tìm kiếm không phân biệt chữ hoa chữ thường?
- Muốn kiểm tra xem tất cả các phần tử trong mảng có thỏa mãn một điều kiện nào đó hay không?
- Và như thế
Để tìm hiểu thêm về mảng JavaScript và cách lưu trữ nhiều mẩu thông tin trong một biến duy nhất, hãy đọc bài viết này