检查 JavaScript 中的数组中是否存在多个值

检查 JavaScript 中的数组中是否存在多个值

Check if Multiple Values exist in Array in JavaScript

检查数组中是否存在多个值:

  1. 使用该every()方法迭代值数组。
  2. 在每次迭代中检查该值是否包含在另一个数组中。
  3. 如果数组中存在所有值,该every方法将返回true
索引.js
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()方法来检查该值是否包含在另一个数组中。

索引.js
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

如果您需要经常这样做,请定义一个可重用的函数。

索引.js
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使用对象检查数组中是否有多个值

这是一个三步过程:

  1. 将数组转换为Set对象。
  2. 使用该every()方法迭代值数组。
  3. 使用Set.has()方法检查每个值是否在Set.
索引.js
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).

index.js
// 👇️ 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:

  1. Use the every() method to iterate over the array of values.
  2. Use the indexOf method to check if each value is contained in the other
    array.
  3. If all values exist in the array, the every method will return true.
index.js
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.

The 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.

index.js
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: