Check If Multiple Values Exist In An Array In JavaScript

check if multiple values exist in an array in JavaScript
#image_title

To check if multiple values exist in an array in JavaScript –

  1. Use the every() method to iterate over the array.
  2. In each iteration, check if the values are present in the array.
  3. If all the values are present in the array, the every() method returns true, else it will return false.

every() To Check If Multiple Values Exist In An Array In JavaScript

Example

Check if the value “3” and “4” exist in the array:

var arr = ["1", "2", "3", "4", "5"];

var values = ["3", "4"];

function checkValues(arr, values) {

  return values.every(function(v) {

     return arr.indexOf(v) >= 0;

});

}

console.log(checkValues(arr, values)); //true

In the above code, the every() method iterates over the values array.

In each iteration, it checks if the current value is present in the arr array.

If all the values are present in the arr array, it returns true, else it returns false.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

If all the values are not present in the arr array, it returns false.

For example, if the value “6” is not present in the arr array, it would return false.

Check if the value “3”, “4”, and “6” exist in the array:

var arr = ["1", "2", "3", "4", "5"];

var values = ["3", "4","6″];

function checkValues(arr, values) {

   return values.every(function(v) {

     return arr.indexOf(v) >= 0;

});

}

console.log(checkValues(arr, values)); //false

In the above code, the value “6” is not present in the arr array, hence it would return false.

Using includes() To Check If Multiple Values Exist In An Array

You can also use the includes() method to check if multiple values exist in an array.

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Example

Check if the value “3” and “4” exist in the array:

var arr = ["1", "2", "3", "4", "5"];

var values = ["3", "4"];

function checkValues(arr, values) {

  return values.every(function(v) {

    return arr.includes(v);

});

}

console.log(checkValues(arr, values)); //true

In the above code, the every() method iterates over the values array.

In each iteration, it checks if the current value is present in the arr array using the includes() method.

If all the values are present in the arr array, it returns true, else it returns false.

The includes() method determines whether an array includes a given element, returning true or false as appropriate.

If all the values are not present in the arr array, it returns false.

For example, if the value “6” is not present in the arr array, it would return false.

Check if the value “3”, “4”, and “6” exist in the array:

var arr = ["1", "2", "3", "4", "5"];

var values = ["3", "4","6″];

function checkValues(arr, values) {

   return values.every(function(v) {

      return arr.includes(v);

});

}

console.log(checkValues(arr, values)); //false

for…of Loop To Check If Multiple Values Exist In An Array In JavaScript

Here’s an example using a for...of loop to check if multiple values exist in an array:

const array = [1, 2, 3, 4, 5];
const valuesToCheck = [2, 4, 6];

let allExist = true;

for (const value of valuesToCheck) {
  if (!array.includes(value)) {
    allExist = false;
    break;
  }
}

console.log(allExist); // Output: false

In this example, we iterate over each value in the valuesToCheck array using a for...of loop. For each value, we use the includes() method to check if it exists in the array.

If a value is not found in the array, we set the allExist variable to false and break out of the loop using the break statement. This means that not all values exist in the array.

Finally, we output the value of the allExist variable, which indicates whether all values from valuesToCheck exist in the array. In the example above, since 6 is not present in the array, the allExist variable is set to false.

Using filter() Method

Here’s an example using the filter() method to check if multiple values exist in an array:

const array = [1, 2, 3, 4, 5];
const valuesToCheck = [2, 4, 6];

const existingValues = valuesToCheck.filter(value => array.includes(value));

const allExist = existingValues.length === valuesToCheck.length;

console.log(allExist); // Output: false

In this example, we use the filter() method to create a new array called existingValues that contains only the values from valuesToCheck that exist in the array.

Inside the filter() callback function, we check if each value exists in the array using the includes() method. If a value is found, it is added to the existingValues array.

Finally, we compare the length of existingValues with the length of valuesToCheck. If they are equal, it means all values from valuesToCheck exist in the array, and allExist is set to true. Otherwise, it means at least one value is missing, and allExist is set to false.

In the example above, since 2 and 4 exist in the array, but 6 does not, the allExist variable is set to false.

Using The Set Object

The Set object is another approach to check if multiple values exist in an array in JavaScript. Here’s an example using the Set object:

const array = [1, 2, 3, 4, 5];
const valuesToCheck = [2, 4, 6];

const arraySet = new Set(array);
const valuesToCheckSet = new Set(valuesToCheck);

const allExist = valuesToCheck.every(value => arraySet.has(value));

console.log(allExist); // Output: false

In this example, we create two Set objects: arraySet from the original array and valuesToCheckSet from the values to check. The Set object stores unique values, so it automatically eliminates duplicates.

We then use the every() method on the valuesToCheck array, where the callback function checks if each value exists in the arraySet using the has() method. The every() method returns true if all the values pass the condition.

Finally, we output the value of allExist, which indicates whether all values from valuesToCheck exist in the array. In the example above, since 6 is not present in the arraySet, the allExist variable is set to false.

Using the Set object provides a concise and efficient way to check for the existence of multiple values in an array, taking advantage of its uniqueness and the has() method for quick lookups.

Conclusion

In conclusion, we have explored different methods to check if multiple values exist in an array in JavaScript. Each method offers its own approach and flexibility based on the specific requirements of your code. Here’s a summary of the methods discussed:

  1. The every() method: This method checks if every element in an array satisfies a given condition. It can be combined with the indexOf() method to determine if all values from a separate array exist in the target array.
  2. The includes() method: This method checks if a specific value is present in an array. It can be used in conjunction with various looping methods to determine the existence of specific values.
  3. The filter() method: This method creates a new array containing elements that pass a given condition. By using the includes() method within the filter() callback function, we can extract the values that exist in both the target array and the values array, allowing us to determine if all values from the values array exist in the target array.
  4. The for...of loop: This loop provides a straightforward way to iterate through the elements of an array. By using the includes() method within the loop, we can check for the existence of specific values and set a flag accordingly.
  5. The Set object: By creating a Set object from the array and values to check, we can leverage its unique values feature. Using the has() method, we can check if each value exists in the Set, and the every() method allows us to determine if all values pass the condition.

These methods provide different options for checking the existence of multiple values in an array, and the choice of method depends on factors such as readability, performance, and specific requirements of your code. Consider the characteristics of each method and choose the one that best suits your needs.

Leave a Reply