How To Check If Array Has All Elements Of Another Array in JavaScript?

Check if Array has all Elements of Another Array in JavaScript

Arrays are a data frequently used in JS. If you don’t know how to check if array has all elements of another array in JavaScript, don’t worry. We will give you some methods to do it in this article. Let’s check it out!

Some array methods in Javascript

In JavaScript, an array is a particular variable that stores different elements.

Arrays have several built-in properties and array methods (Called array methods) that we can use to add, remove, iterate or manipulate data according to our needs.

Here are some methods that we can use in this article:

  • push(): Add an element to the end of the array.
  • unshift(): Add an element to the beginning of the array.
  • pop(): Removes the last element in the array.
  • shift(): Removes the first element in the array.

Code:

var arr = [];
arr.push(1); // arr = [1]
arr.unshift(0); // arr = [0, 1]
arr.pop(); // arr = [0]
arr.shift(); // arr = []

every() will check if all elements in the array satisfy the test method inside it.

Code:

const arr1 = [3, 6, 9, 12, 15];
arr1.every(item => item % 3 == 0); // true

const arr2 = [3, 6, 7, 12, 14];
arr2.every(el => el % 2 == 0); // false

map() will take a parameter as a test method, but this test method does not necessarily return true/false.

find() also takes a test method as a parameter and returns the first element that satisfies the test method. If there are no elements, it returns undefined.

includes() takes an el parameter and will return true if the array contains the el element, false otherwise.

Check if array has all elements of another array in JavaScript

Use every() together with includes() method

This method will use every() method and the includes() method. The developer will use every() method to iterate through each element of the array and check with the function passed as a parameter.

Syntax:

every(function(currentValue, index, arr), thisValue)

Parameters:

  • function: required
  • currentValue: required
  • index: optional
  • arr: optional
  • thisValue: optional

And in the function passed in, we will use the includes() method to compare each element with the available array of requests.

Syntax:

includes(searchElement, fromIndex)

Parameters:

  • searchElement: the value to search for
  • fromIndex: optional

Code:

var parent = [1, 2, 3, 4];
var child = [1, 4];
console.log(child.every(item => parent.includes(item)));

Output:

true

So we can check if each element in “child” array is in “parent”.

Use indexOf() method

indexOf() is used to check if a string contains a specific substring. If the program can find the substring in the string, the indexOf() function returns the position of the first occurrence of the string element. Otherwise, the function will return -1. So, in this case, we will apply more for loop to check. Check out the example below.

Code:

var parent = [1, 2, 3, 4];
var child = [1, 4];
var check = true;

for (let i = 0; i < child.length; i++) {
	if (parent.indexOf(child[i]) === -1) {
		check = false;
		break;
	}
}

console.log(check);

Output:

true

In the above example, we use a for loop to iterate over all the elements of the child array and use the indexOf() method with the word of the parent array. If the value of the child array is not in the parent array, the return value will be -1, and the check variable will change to false.

Summary

To summarize, this article shows us how to check if array has all elements of another array in JavaScript. You can use every() together with the includes() method or indexOf() method. Good luck to you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *