How to check whether multiple values exist within an Javascript array

前端 未结 10 1990
生来不讨喜
生来不讨喜 2020-11-28 04:32

So, I\'m using Jquery and have two arrays both with multiple values and I want to check whether all the values in the first array exist in the second.

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 05:08

    A one-liner to test that all of the elements in arr1 exist in arr2...

    With es6:

    var containsAll = arr1.every(i => arr2.includes(i));
    

    Without es6:

    var containsAll = arr1.every(function (i) { return arr2.includes(i); });
    

提交回复
热议问题