检查 JavaScript 中的数组中是否存在多个值
Check if Multiple Values exist in Array in JavaScript
检查数组中是否存在多个值:
- 使用该
every()
方法迭代值数组。 - 在每次迭代中检查该值是否包含在另一个数组中。
- 如果数组中存在所有值,该
every
方法将返回true
。
const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleExist = values.every(value => { return arr.includes(value); }); console.log(multipleExist); // 👉️ true
我们传递给Array.every方法的函数
会针对每个元素进行调用,直到它返回一个假值或遍历整个数组。
在每次迭代中,我们使用
Array.includes()方法来检查该值是否包含在另一个数组中。
const arr = ['one', 'two', 'three', 'four']; console.log(arr.includes('one')); // 👉️ true console.log(arr.includes('two')); // 👉️ true console.log(arr.includes('abc')); // 👉️ false
如果即使数组中不包含单个值,该includes()
方法也会返回false
,这会导致该every
方法短路并返回
false
。
如果您需要经常这样做,请定义一个可重用的函数。
function multipleInArray(arr, values) { return values.every(value => { return arr.includes(value); }); } console.log(multipleInArray([1, 2, 3], [1, 3])); // 👉️ true console.log(multipleInArray([1, 2, 3], [1, 10])); // 👉️ false
该multipleInArray
函数接受一个数组和一组值,并检查数组中是否存在指定的值。
如果所有值都满足条件,则函数返回true
,否则返回 false。
Set
使用对象检查数组中是否有多个值
这是一个三步过程:
- 将数组转换为
Set
对象。 - 使用该
every()
方法迭代值数组。 - 使用
Set.has()
方法检查每个值是否在Set
.
function multipleInArray(arr, values) { const set = new Set(arr); return values.every(value => { return set.has(value); }); } // 👇️ true console.log(multipleInArray(['bobby', 'hadz', 'com'], ['bobby', 'hadz'])); // 👇️ false console.log(multipleInArray([1, 2, 3], [1, 10]));
We used the Set()
constructor to
convert the array to a Set
object to
remove all the duplicates (if any).
// 👇️ Set(3) { 1, 2, 3 } console.log(new Set([1, 1, 2, 2, 3, 3]));
Set
objects are collections of unique elements, so if any duplicates exist in
the array, they get dropped once passed to the Set()
constructor.
The next step is to use the Array.every()
method to iterate over the array of
values.
On each iteration, we use the
Set.has()
method to check if the current value is contained in the Set
object.
The Set.has
method returns true
if the value is contained in the Set
and
false
otherwise.
# Check if Multiple Values exist in an Array using indexOf()
This is a three-step process:
- Use the
every()
method to iterate over the array of values. - Use the
indexOf
method to check if each value is contained in the other
array. - If all values exist in the array, the
every
method will returntrue
.
const arr = ['one', 'two', 'three', 'four']; const values = ['one', 'two']; const multipleInArray = values.every(value => { return arr.indexOf(value) !== -1; }); console.log(multipleInArray); // 👉️ true
This is very similar to our previous example. However, this time we used the
Array.indexOf method.
indexOf
method checks if a value is contained in an array and returns the index
of the first occurrence of the value in the array.If the value is not contained in the array, the method returns -1
.
If the indexOf
method doesn’t return -1
, then the value is contained in the
array.
Here is a reusable function that achieves the same result.
function multipleInArray(arr, values) { return values.every(value => { return arr.indexOf(value) !== -1; }); } // 👇️ true console.log(multipleInArray([1, 2, 3], [1, 2])); // 👇️ false console.log(multipleInArray([1, 2, 3], [4, 2]));
The function takes the array and the collection of values as parameters and
returns true
if all of the values are contained in the array and false
otherwise.
# Additional Resources
You can learn more about the related topics by checking out the following
tutorials:
- Check if an Array contains Duplicates in JavaScript
- Count the Duplicates in an Array in JavaScript
- Prevent adding Duplicates to an Array in JavaScript
- Check if all Values in Array are Equal in JavaScript
- Check if Array has all Elements of Another Array – JavaScript
- TypeError: Cannot use ‘in’ operator to search for ‘X’ in ‘Y’