Check if array contains all elements of another array

前端 未结 5 1203
后悔当初
后悔当初 2020-11-30 08:27

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         


        
5条回答
  •  情深已故
    2020-11-30 09:02

    You can use .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));
    console.log(checker(array3, array1));

提交回复
热议问题