Check If An Array Has All Elements Of Another Array In JavaScript

To check if an array has all elements of another array in JavaScript, you can use the Array.every() method. The Array.every() method returns a Boolean value that indicates whether all elements in an array pass the test implemented by a provided function.

check if an array has all elements of another array

Check If An Array Has All Elements Of Another Array In JavaScript

If you need to check if an array has all elements of another array, and both arrays may have duplicates, you can use the following approach:

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var checkArr1ElementsInArr2 = arr1.every(function(item){

  return arr2.includes(item);

});

console.log(checkArr1ElementsInArr2); //true

In the above example, we have two arrays: arr1 and arr2. We want to know if arr1 has all the elements of arr2. To do that, we use the Array.every() method. The Array.every() method returns a Boolean value that indicates whether all elements in an array pass the test implemented by a provided function.

In our case, we use the provided function to check if every element of arr1 is present in arr2.

If all the elements of arr1 are present in arr2, then Array.every() returns true. Otherwise, it returns false.

Let’s check if all elements of arr2 are present in arr1.

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var checkArr2ElementsInArr1 = arr2.every(function(item){

  return arr1.includes(item);

});

console.log(checkArr2ElementsInArr1); //false

As you can see, we get false as a result because not all elements of arr2 are present in arr1.

You can also use the indexOf() method to check if an array has all elements of another array. The indexOf() method returns the first index at which a given element can be found in an array, or -1 if it is not present in the array.

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var checkArr1ElementsInArr2 = arr1.every(function(item){

  return arr2.indexOf(item) > -1;

});

console.log(checkArr1ElementsInArr2); //true

In the above example, we use the indexOf() method to check if every element of arr1 is present in arr2.

If all the elements of arr1 are present in arr2, then Array.every() returns true. Otherwise, it returns false.

Let’s check if all elements of arr2 are present in arr1.

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var checkArr2ElementsInArr1 = arr2.every(function(item){

   return arr1.indexOf(item) > -1;

});

console.log(checkArr2ElementsInArr1); //false

In the above example, we use the indexOf() method to check if every element of arr2 is present in arr1.

As you can see from the output, not all the elements of arr2 are present in arr1. Therefore, Array.every() returns false.

You can also use the Set data structure to check if an array has all elements of another array. The Set object lets you store unique values of any type, whether primitive values or object references.

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var set1 = new Set(arr2);

var checkArr1ElementsInArr2 = arr1.every(function(item){

  return set1.has(item);

});

console.log(checkArr1ElementsInArr2); //true

In the above example, we use Set to check if all elements of arr1 are present in arr2.

If all the elements of arr1 are present in arr2, then Array.every() returns true. Otherwise, it returns false.

Let’s check if all elements of arr2 are present in arr1.

var arr1 = [‘a’, ‘b’, ‘c’];

var arr2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’];

var set1 = new Set(arr1);

var checkArr2ElementsInArr1 = arr2.every(function(item){

  return set1.has(item);

});

console.log(checkArr2ElementsInArr1); //false

As you can see from the output, not all elements of arr2 are present in arr1. Therefore, Array.every() returns false.

Conclusion

In this article, we saw how to check if an array has all elements of another array in JavaScript using the Array.every() method, the indexOf() method, and the Set data structure.

You can use any of these methods to check if an array has all elements of another array in JavaScript.

I hope you found this article helpful. Thanks for reading!

Leave a Reply