Check if array contains all elements of another array

JavascriptArrays

Javascript Problem Overview


I want a function that returns true if and only if a given array includes all the elements of a given "target" array. As follows.

const target = [ 1, 2, 3,    ];
const array1 = [ 1, 2, 3,    ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2,       ]; // false

How can I accomplish the above result?

Javascript Solutions


Solution 1 - Javascript

You can combine the .every() and .includes() methods:

let array1 = [1,2,3],
    array2 = [1,2,3,4],
    array3 = [1,2];

let checker = (arr, target) => target.every(v => arr.includes(v));

console.log(checker(array2, array1));  // true
console.log(checker(array3, array1));  // false

Solution 2 - Javascript

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. Stands to reason that if you call every() on the original array and supply to it a function that checks if every element in the original array is contained in another array, you will get your answer. As such:

const ar1 = ['a', 'b'];
const ar2 = ['c', 'd', 'a', 'z', 'g', 'b'];

if(ar1.every(r => ar2.includes(r))){
  console.log('Found all of', ar1, 'in', ar2);
}else{
  console.log('Did not find all of', ar1, 'in', ar2);
}

Solution 3 - Javascript

You can try with Array.prototype.every():

>The every() method tests whether all elements in the array pass the test implemented by the provided function.

and Array.prototype.includes():

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

var mainArr = [1,2,3];
function isTrue(arr, arr2){
  return arr.every(i => arr2.includes(i));
}
console.log(isTrue(mainArr, [1,2,3]));
console.log(isTrue(mainArr, [1,2,3,4]));
console.log(isTrue(mainArr, [1,2]));

Solution 4 - Javascript

I used Purely Javascript.

function checkElementsinArray(fixedArray,inputArray)
{
	var fixedArraylen = fixedArray.length;
	var inputArraylen = inputArray.length;
	if(fixedArraylen<=inputArraylen)
	{
		for(var i=0;i<fixedArraylen;i++)
		{
			if(!(inputArray.indexOf(fixedArray[i])>=0))
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
	return true;
}

console.log(checkElementsinArray([1,2,3], [1,2,3]));
console.log(checkElementsinArray([1,2,3], [1,2,3,4]));
console.log(checkElementsinArray([1,2,3], [1,2]));

Solution 5 - Javascript

If you are using ES5, then you can simply do this.

targetArray =[1,2,3]; 
array1 = [1,2,3]; //return true
array2 = [1,2,3,4]; //return true
array3 = [1,2] //return false

console.log(targetArray.every(function(val) { return array1.indexOf(val) >= 0; })); //true
 console.log(targetArray.every(function(val) { return array2.indexOf(val) >= 0; })); // true
 console.log(targetArray.every(function(val) { return array3.indexOf(val) >= 0; }));// false
 

Solution 6 - Javascript

reduce can be used here as well (but it has O = (N * M) difficulty):

const result = target.reduce((acc, el) => {
    return acc && array.includes(el)
}, true);

To solve this in more efficient way(O = N + M):

const myMap = new Map();

array.forEach(element => myMap.set(element);

const result = target.reduce((acc, el) => {
   return acc && myMap.has(el)
}, true);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlakshay bakshiView Question on Stackoverflow
Solution 1 - JavascriptMohammad UsmanView Answer on Stackoverflow
Solution 2 - JavascriptcodemonkeyView Answer on Stackoverflow
Solution 3 - JavascriptMamunView Answer on Stackoverflow
Solution 4 - JavascriptPankaj RevagadeView Answer on Stackoverflow
Solution 5 - JavascriptzetawarsView Answer on Stackoverflow
Solution 6 - JavascriptUladz KhaView Answer on Stackoverflow