如何检查Javascript数组中是否存在多个值

时间:2022-03-19 07:42:58

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.

所以,我正在使用Jquery,并且有两个数组都有多个值,我想检查第一个数组中的所有值是否都存在于第二个数组中。

For instance, example 1...

例如,示例1 ......

Array A contains the following values

数组A包含以下值

34, 78, 89

34,78,89

Array B contains the following values

数组B包含以下值

78, 67, 34, 99, 56, 89

78,67,34,99,56,89

This would return true

这将返回真实

...example 2:

......例2:

Array A contains the following values

数组A包含以下值

34, 78, 89

34,78,89

Array B contains the following values

数组B包含以下值

78, 67, 99, 56, 89

78,67,99,56,89

This would return false

这将返回false

...example 3:

......例3:

Array A contains the following values

数组A包含以下值

34, 78, 89

34,78,89

Array B contains the following values

数组B包含以下值

78, 89

78,89

This would return false

这将返回false

So far I have tried to solve this by:

到目前为止,我试图解决这个问题:

  1. Extending Jquery with a custom 'compare' method to compare the two arrays. Problem is this only returns true when the arrays are identical and as you can see from example 1 I want it to return true even if they aren't identical but at least contain the value
  2. 使用自定义'compare'方法扩展Jquery以比较两个数组。问题是当数组相同时这只返回true,你可以从例1看到我希望它返回true,即使它们不相同但至少包含值
  3. using Jquerys .inArray function, but this only checks for one value in an array, not multiple.
  4. 使用Jquerys .inArray函数,但这只检查数组中的一个值,而不是多个。

Any light that anyone could throw on this would be great.

任何人都可以投入任何光线都会很棒。

6 个解决方案

#1


46  

function containsAll(needles, haystack){ 
  for(var i = 0 , len = needles.length; i < len; 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

#2


48  

Native JavaScript solution

原生JavaScript解决方案

var success = array_a.every(function(val) {
    return array_b.indexOf(val) !== -1;
});

You'll need compatibility patches for every and indexOf if you're supporting older browsers, including IE8.

如果您支持旧版浏览器(包括IE8),则需要为每个和indexOf添加兼容性补丁。


Full jQuery solution

完整的jQuery解决方案

var success = $.grep(array_a, function(v,i) {
    return $.inArray(v, array_b) !== -1;
}).length === array_a.length;

Uses $.grep with $.inArray.

使用带有$ .inArray的$ .grep。


ES2015 Solution

ES2015解决方案

The native solution above can be shortened using ES2015's arrow function syntax and its .includes() method:

可以使用ES2015的箭头函数语法及其.includes()方法缩短上面的本机解决方案:

let success = array_a.every((val) => array_b.includes(val))

#3


14  

I noticed that the question is about solving this with jQuery, but if anyone else who is not limited to jQuery comes around then there is a simple solution using underscore js.

我注意到问题是关于使用jQuery解决这个问题,但是如果其他任何不限于jQuery的人出现,那么使用下划线js就有一个简单的解决方案。

Using underscore js you can do:

使用下划线js,您可以:

_.intersection(ArrayA, ArrayB).length === ArrayA.length;

From the docs:

来自文档:

intersection_.intersection(*arrays) Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

intersection_.intersection(* arrays)计算所有数组交集的值列表。结果中的每个值都存在于每个数组中。

_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); => [1, 2]

_.intersection([1,2,3],[101,2,1,10],[2,1]); => [1,2]

Ergo, if one of the items in ArrayA was missing in ArrayB, then the intersection would be shorter than ArrayA.

因此,如果ArrayA中缺少ArrayA中的某个项目,那么交集将比ArrayA短。

#4


4  

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

一个单行测试arr2中存在arr1中的所有元素...

With es6:

使用es6:

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

Without es6:

没有es6:

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

#5


0  

Try this.

尝试这个。

var arr1 = [34, 78, 89];
var arr2 = [78, 67, 34, 99, 56, 89];

var containsVal = true;
$.each(arr1, function(i, val){
   if(!$.inArray(val, arr2) != -1){
       retVal = false;
       return false;
   }
});

if(containsVal){
    //arr2 contains all the values from arr1 
}

#6


0  

You can use this simple function (renamed variables as per above answer for easy reading):

您可以使用这个简单的函数(根据上面的答案重命名变量以便于阅读):

function contains(haystack, needles) {

    return needles.map(function (needle) { 
        return haystack.indexOf(needle);
    }).indexOf(-1) == -1;
}

#1


46  

function containsAll(needles, haystack){ 
  for(var i = 0 , len = needles.length; i < len; 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

#2


48  

Native JavaScript solution

原生JavaScript解决方案

var success = array_a.every(function(val) {
    return array_b.indexOf(val) !== -1;
});

You'll need compatibility patches for every and indexOf if you're supporting older browsers, including IE8.

如果您支持旧版浏览器(包括IE8),则需要为每个和indexOf添加兼容性补丁。


Full jQuery solution

完整的jQuery解决方案

var success = $.grep(array_a, function(v,i) {
    return $.inArray(v, array_b) !== -1;
}).length === array_a.length;

Uses $.grep with $.inArray.

使用带有$ .inArray的$ .grep。


ES2015 Solution

ES2015解决方案

The native solution above can be shortened using ES2015's arrow function syntax and its .includes() method:

可以使用ES2015的箭头函数语法及其.includes()方法缩短上面的本机解决方案:

let success = array_a.every((val) => array_b.includes(val))

#3


14  

I noticed that the question is about solving this with jQuery, but if anyone else who is not limited to jQuery comes around then there is a simple solution using underscore js.

我注意到问题是关于使用jQuery解决这个问题,但是如果其他任何不限于jQuery的人出现,那么使用下划线js就有一个简单的解决方案。

Using underscore js you can do:

使用下划线js,您可以:

_.intersection(ArrayA, ArrayB).length === ArrayA.length;

From the docs:

来自文档:

intersection_.intersection(*arrays) Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

intersection_.intersection(* arrays)计算所有数组交集的值列表。结果中的每个值都存在于每个数组中。

_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); => [1, 2]

_.intersection([1,2,3],[101,2,1,10],[2,1]); => [1,2]

Ergo, if one of the items in ArrayA was missing in ArrayB, then the intersection would be shorter than ArrayA.

因此,如果ArrayA中缺少ArrayA中的某个项目,那么交集将比ArrayA短。

#4


4  

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

一个单行测试arr2中存在arr1中的所有元素...

With es6:

使用es6:

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

Without es6:

没有es6:

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

#5


0  

Try this.

尝试这个。

var arr1 = [34, 78, 89];
var arr2 = [78, 67, 34, 99, 56, 89];

var containsVal = true;
$.each(arr1, function(i, val){
   if(!$.inArray(val, arr2) != -1){
       retVal = false;
       return false;
   }
});

if(containsVal){
    //arr2 contains all the values from arr1 
}

#6


0  

You can use this simple function (renamed variables as per above answer for easy reading):

您可以使用这个简单的函数(根据上面的答案重命名变量以便于阅读):

function contains(haystack, needles) {

    return needles.map(function (needle) { 
        return haystack.indexOf(needle);
    }).indexOf(-1) == -1;
}