Checking if all value of X array is in Y or not

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

Professor Asked on October 26, 2020 in Android langue.
Add Comment
1 Answer(s)

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)));

Professor Answered on October 26, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.