检查数组是否不包含 JavaScript 中的值
Check if Array Doesn’t include a Value in JavaScript
使用逻辑 NOT (!) 运算符否定对includes()
方法的调用以检查数组是否不包含值。
如果值不在数组中,则对方法的否定调用Array.includes()
将返回,否则。true
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 contained in the array'); } else { // 👇️ this runs console.log('⛔️ the value c is contained in the array'); } // --------------------------------------- // ✅ Check if an object is not contained in an array const arr2 = [{id: 1}, {id: 2}, {id: 3}]; const notContained = arr2.every(obj => { return obj.id !== 4; }); console.log(notContained); // 👉️ true
我们使用
逻辑 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 (!) 运算符首先将值转换为 a
boolean
,然后翻转值。
true
。在所有其他情况下,它返回false
。JavaScript 中的假值是:null
、undefined
、空字符串NaN
、
0
和false
。
如果您必须经常执行测试,请定义一个可重用的函数。
function notIncludes(array, value) { return !array.includes(value); } const arr = ['a', 'b', 'c']; console.log(notIncludes(arr, 'c')); // 👉️ false console.log(notIncludes(arr, 'd')); // 👉️ true console.log(notIncludes(arr, 'z')); // 👉️ true
notIncludes()
函数将一个数组和一个值作为参数,并检查该值是否不包含在数组中。The function returns true
if the value is not contained in the array and
false
otherwise.
You can also use the indexOf()
method to check if a value is not contained in
an array.
Check if Array Doesn’t contain a Value using indexOf()
#
To check if an array doesn’t contain a value:
- Use the
indexOf()
method to get the index of the value in the array. - If the method returns
-1
, the array doesn’t contain the value.
const arr = ['a', 'b', 'c']; if (arr.indexOf('d') === -1) { // 👇️ this runs console.log('The value d is NOT contained in the array'); } else { console.log('The value d is contained in the array'); } console.log(arr.indexOf('c')); // 👉️ 2 console.log(arr.indexOf('z')); // 👉️ -1
The
Array.indexOf
method returns the index of the first occurrence of the supplied value in the
array.
If the value is not contained in the array, the method returns -1
.
Our if
statement checks if the Array.indexOf()
method returned -1
.
-1
, then the value is not contained in the array.If you have to perform the test often, define a reusable function.
function notIncludes(array, value) { return array.indexOf(value) === -1; } const arr = ['a', 'b', 'c']; console.log(notIncludes(arr, 'c')); // 👉️ false console.log(notIncludes(arr, 'd')); // 👉️ true console.log(notIncludes(arr, 'z')); // 👉️ true
The function takes an array and a value as parameters and returns true
if the
value is not contained in the array.
If you need to check if an object is not contained in an array, use the
Array.every()
method.
Check if an object is not contained in an array in JavaScript #
To check if an object is not contained in an array:
- Use the
Array.every()
method to iterate over the array. - Check if each object doesn’t have a property equal to the specific value.
- The
every()
method will returntrue
if the object is not in the array.
const arr2 = [{id: 1}, {id: 2}, {id: 3}]; const notContained = arr2.every(obj => { return obj.id !== 4; }); console.log(notContained); // 👉️ true if (notContained) { console.log('The object is NOT contained in the array'); } else { console.log('The object is contained in the array'); }
The function we passed to the
Array.every()
method gets called with each element of the array.
Array.every()
method returns true
, otherwise, false
is returned.On each iteration, we check if the current object doesn’t have a specific value
and return the result.
If the callback function we passed to the Array.every()
method returns a falsy
value, then Array.every()
short-circuits also returning false
.
If the Array.every()
method returns true
, then the object isn’t contained in
the array.
If the method returns false
, the object is contained in the array.