JavaScript 中检查数组是否不包含某个值
要检查数组是否不包含值,请使用逻辑 NOT !
运算符来否定对 includes()
方法的调用。 NOT !
运算符在对真值调用时返回 false,反之亦然。
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');
}
我们使用逻辑 NOT !
运算符来否定对 Array.includes
方法的调用。
这种方法允许我们检查特定值是否不包含在数组中。
我们的第一个示例检查值 d
是否不包含在数组中并返回 true。
const arr = ['a', 'b', 'c'];
const notIncludesD = !arr.includes('d');
console.log(notIncludesD); // 👉️ true
const notIncludesC = !arr.includes('c');
console.log(notIncludesC); // 👉️ false
字符串 c 包含在数组中,因此表达式返回 false。
以下是使用逻辑 NOT !
运算符的更多示例。
console.log(!true); // 👉️ false
console.log(!false); // 👉️ true
console.log(!'hello'); // 👉️ false
console.log(!''); // 👉️ true
console.log(!null); // 👉️ true
我们可以想象逻辑 NOT !
运算符首先将值转换为布尔值,然后翻转值。
当我们否定一个假值时,运算符返回真。 在所有其他情况下,它返回 **
false
**。
JavaScript 中的假值有:null 、undefined 、空字符串 、NaN 、0 和 false。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
将 NumPy 数组转换为 Pandas DataFrame
发布时间:2024/04/21 浏览次数:111 分类:Python
-
本教程介绍了如何使用 pandas.DataFrame()方法从 NumPy 数组生成 Pandas DataFrame。
如何将 Pandas Dataframe 转换为 NumPy 数组
发布时间:2024/04/20 浏览次数:176 分类:Python
-
本教程介绍如何将 Pandas Dataframe 转换为 NumPy 数组的方法,例如 to_numpy,value 和 to_records
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。