0

I have two arrays

var A = [1,2,3,4,5];
var B = [1,2,3,4,5,6,7,8,9];

Now I want to check if all elements of A are exist in B or not like this

var A = [1,2,3,4,5];
var B = [1,2,3,4,5,6,7,8,9];
// true


var A = [1,2,3,4,5];
var B = [1,2,3,4,5];
// True

var A = [1,2,3,4,5];
var B = [1,2,3,5,6,7,8,9];
// False

I have tried using some like this

if(A.some(item => B.includes(item)) {
     return true;
}

But it filters the array and returns true if one of the value is true

  • Does this answer your question? [Check if array contains all elements of another array](https://stackoverflow.com/questions/53606337/check-if-array-contains-all-elements-of-another-array) – Ivar Jun 01 '20 at 10:52
  • Check if this following helps you, its similar implementation. https://stackoverflow.com/questions/8628059/check-if-every-element-in-one-array-is-in-a-second-array – Wahab Shah Jun 01 '20 at 10:54
  • also you have a typo in your last code snippet, you have forgotten to close the first bracket which comes right after if keyword. – Subhan Asadli Jun 01 '20 at 10:59

1 Answers1

1

Use every()

var A1 = [1,2,3,4,5];
var B1 = [1,2,3,4,5,6,7,8,9];
var A2 = [1,2,3,4,5];
var B2 = [1,2,3,4,5];
var A3 = [1,2,3,4,5];
var B3 = [1,2,3,5,6,7,8,9];

console.log(A1.every(item => B1.includes(item)));
console.log(A2.every(item => B2.includes(item)));
console.log(A3.every(item => B3.includes(item)));
Nikita Madeev
  • 4,284
  • 9
  • 20