所以,我使用jQuery,并且都具有多个值,我想检查是否全部第一个数组中的值在第二个存在。
两个数组有关实例,例如1 ...
数组A包含下列值
34,78,89
数组b中包含以下值
78,67,34,99,56,89
这将返回真正
...示例2:
数组A包含下列值
34,78,89
数组b中包含以下值
78,67,99,56,89
这将返回假
...例子3:
数组A包含下列值
34,78,89
数组b中包含以下值
78,89
这将返回假
到目前为止,我已经试图解决这个:
这是任何人都可以扔在这的任何光线将是巨大的。
解决方案 函数containsAll(针,干草堆){ 对于(VAR I = 0,LEN = needles.length; I< LEN,我++){ 如果($ inArray(针[I],草垛)== -1)返回false; } 返回true;}containsAll([34,78,89],[78,67,34,99,56,89]); //真containsAll([34,78,89],[78,67,99,56,89]); //假containsAll([34,78,89],[78,89]); //假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.
For instance, example 1...
Array A contains the following values
34, 78, 89
Array B contains the following values
78, 67, 34, 99, 56, 89
This would return true
...example 2:
Array A contains the following values
34, 78, 89
Array B contains the following values
78, 67, 99, 56, 89
This would return false
...example 3:
Array A contains the following values
34, 78, 89
Array B contains the following values
78, 89
This would return false
So far I have tried to solve this by:
Any light that anyone could throw on this would be great.
解决方案 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
更多推荐
如何检查是否一个JavaScript数组中存在多个值
发布评论