Kiểm tra xem mảng không bao gồm giá trị #
Để kiểm tra xem một mảng không bao gồm một giá trị, hãy sử dụng toán tử logic không [!] Để phủ định cuộc gọi đến phương thức includes[]
. Toán tử không [!] Trả về false
khi được gọi trên giá trị true
và ngược lại.
Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false if [notIncludesC] { console.log['✅ the value c is NOT included in the array']; } else { console.log['⛔️ the value c is included in the array']; }
Chúng tôi đã sử dụng toán tử logic không [!] Để phủ định các cuộc gọi đến phương thức mảng.
Cách tiếp cận này cho phép chúng tôi kiểm tra xem một giá trị cụ thể không có trong mảng.
Ví dụ đầu tiên của chúng tôi kiểm tra xem giá trị d
không được chứa trong mảng và trả về true
.
Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Chuỗi
1 được chứa trong mảng, do đó biểu thức trả vềCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
false
.Dưới đây là một số ví dụ khác về việc sử dụng toán tử logic không [!].
Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Bạn có thể tưởng tượng rằng toán tử logic không [!] Trước tiên chuyển đổi giá trị thành
3 và sau đó lật giá trị.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Khi bạn phủ định giá trị giả, toán tử trả về true
. Trong tất cả các trường hợp khác, nó trả về false
.
Các giá trị giả trong JavaScript là:
6,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
7, chuỗi trống,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
8,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
9 vàCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
false
.Đọc thêm #
- Phần tử mảng.push [] nếu không tồn tại bằng cách sử dụng javascript
- Kiểm tra xem mảng có chứa các phần tử trống trong javascript không
- Kiểm tra xem mảng có chứa một đối tượng trong javascript không
- Kiểm tra xem một mảng có chứa các bản sao trong JavaScript không
- Kiểm tra xem mảng chỉ chứa các số trong javascript
Kiểm tra xem giá trị không có trong mảng #
Để kiểm tra xem giá trị không nằm trong mảng mảng, hãy sử dụng toán tử logic không [!] Để phủ định cuộc gọi đến phương thức Copied!includes[]
, ví dụ:
2. Biểu thức sẽ trả về console.log[!true]; // 👉️ false
console.log[!false]; // 👉️ true
console.log[!'hello']; // 👉️ false
console.log[!'']; // 👉️ true
console.log[!null]; // 👉️ true
true
nếu giá trị không được bao gồm trong mảng và false
nếu không.
Copied!
const arr = ['a', 'b', 'c']; if [!arr.includes['z']] { console.log['✅ value is not in array']; } else { console.log['⛔️ value is in array']; }
Chúng tôi đã sử dụng toán tử logic không [!]
Phương thức bao gồm lấy giá trị làm tham số và trả về true
nếu giá trị được chứa trong mảng.
Vì chúng tôi muốn kiểm tra xem giá trị không có trong mảng, chúng tôi phải phủ định [!] Kết quả.
Dưới đây là một số ví dụ về việc sử dụng toán tử logic không [!].
Copied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
Bạn có thể tưởng tượng rằng toán tử logic không [!] Trước tiên chuyển đổi giá trị thành
3 và sau đó lật giá trị.Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
Khi bạn phủ định giá trị giả, toán tử trả về true
, trong tất cả các trường hợp khác, nó sẽ trả về false
.
Các giá trị giả là:
6,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
7, chuỗi trống,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
8,Copied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
9 vàCopied!
const arr = ['a', 'b', 'c']; const notIncludesD = !arr.includes['d']; console.log[notIncludesD]; // 👉️ true const notIncludesC = !arr.includes['c']; console.log[notIncludesC]; // 👉️ false
false
.Phương pháp Copied!includes[]
không được hỗ trợ trong Internet Explorer. Nếu bạn phải hỗ trợ trình duyệt, hãy sử dụng phương thức
5 thay thế.const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
Một cách tiếp cận khác là sử dụng phương pháp mảng.indexof.
Kiểm tra xem giá trị không có trong mảng bằng cách sử dụng indexof #
Để kiểm tra xem giá trị không nằm trong mảng mảng, hãy sử dụng phương thức Copied! Copied! Copied! Copied!
6, ví dụ: const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
7. Nếu phương thức const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
5 trả về const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
9, thì giá trị không được chứa trong mảng.const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
Copied!
// Supported in IE const arr = ['a', 'b', 'c']; if [arr.indexOf['z'] === -1] { console.log['✅ value is not in array']; } else { console.log['⛔️ value is in array']; }
Phương thức Copied! Copied!
5 trả về chỉ số của lần xuất hiện đầu tiên của giá trị trong một mảng hoặc const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
9 nếu giá trị không được chứa trong mảng.const arr = ['a', 'b', 'c'];
if [!arr.includes['z']] {
console.log['✅ value is not in array'];
} else {
console.log['⛔️ value is in array'];
}
Câu lệnh
2 của chúng tôi kiểm tra xem phương thức đã trả vềCopied!
console.log[!true]; // 👉️ false console.log[!false]; // 👉️ true console.log[!'hello']; // 👉️ false console.log[!'']; // 👉️ true console.log[!null]; // 👉️ true
9, nếu có, chúng tôi có thể kết luận rằng giá trị không nằm trong mảng.Copied!
const arr = ['a', 'b', 'c']; if [!arr.includes['z']] { console.log['✅ value is not in array']; } else { console.log['⛔️ value is in array']; }
Đọc thêm #
- Kiểm tra xem giá trị không bằng 0 bằng cách sử dụng javascript
- Cách kiểm tra xem giá trị có phải là số trong JavaScript không