How to check whether multiple values exist within an Javascript array

前端 未结 10 1947
生来不讨喜
生来不讨喜 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:27

    function containsAll(needles, haystack){ 
      for(var i = 0; i < needles.length; i++){
         if($.inArray(needles[i], haystack) == -1) return false;
      }
      return true;
    }
    
    containsAll([34, 78, 89], [78, 67, 34, 99, 56, 89]); // true
    containsAll([34, 78, 89], [78, 67, 99, 56, 89]); // false
    containsAll([34, 78, 89], [78, 89]); // false
    

提交回复
热议问题