检查数组是否包含另一个数组的所有元素

问题描述 投票:0回答:11

我想要一个当且仅当给定数组包含给定“目标”数组的所有元素时返回

true
的函数。如下。

const target = [ 1, 2, 3,    ];
const array1 = [ 1, 2, 3,    ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2,       ]; // false

如何才能达到上述效果?

javascript arrays
11个回答
318
投票

您可以结合使用

.every()
.includes()
方法:

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));  // true
console.log(checker(array3, array1));  // false


51
投票

every()
方法测试数组中的所有元素是否通过所提供函数实现的测试。它返回一个布尔值。按理说,如果你在原始数组上调用
every()
并为其提供一个函数来检查原始数组中的每个元素是否包含在另一个数组中,你就会得到答案。因此:

const ar1 = ['a', 'b'];
const ar2 = ['c', 'd', 'a', 'z', 'g', 'b'];

if(ar1.every(r => ar2.includes(r))){
  console.log('Found all of', ar1, 'in', ar2);
}else{
  console.log('Did not find all of', ar1, 'in', ar2);
}


32
投票

您可以尝试使用

Array.prototype.every()
:

every()
方法测试数组中的所有元素是否通过提供的函数实现的测试。

Array.prototype.includes()

includes()
方法确定数组是否包含某个元素,根据情况返回 true 或 false。

var mainArr = [1,2,3];
function isTrue(arr, arr2){
  return arr.every(i => arr2.includes(i));
}
console.log(isTrue(mainArr, [1,2,3]));
console.log(isTrue(mainArr, [1,2,3,4]));
console.log(isTrue(mainArr, [1,2]));


2
投票

我使用纯Javascript。

function checkElementsinArray(fixedArray,inputArray)
{
    var fixedArraylen = fixedArray.length;
    var inputArraylen = inputArray.length;
    if(fixedArraylen<=inputArraylen)
    {
        for(var i=0;i<fixedArraylen;i++)
        {
            if(!(inputArray.indexOf(fixedArray[i])>=0))
            {
                return false;
            }
        }
    }
    else
    {
        return false;
    }
    return true;
}

console.log(checkElementsinArray([1,2,3], [1,2,3]));
console.log(checkElementsinArray([1,2,3], [1,2,3,4]));
console.log(checkElementsinArray([1,2,3], [1,2]));

1
投票

如果你使用的是 ES5,那么你可以简单地这样做。

targetArray =[1,2,3]; 
array1 = [1,2,3]; //return true
array2 = [1,2,3,4]; //return true
array3 = [1,2] //return false

console.log(targetArray.every(function(val) { return array1.indexOf(val) >= 0; })); //true
 console.log(targetArray.every(function(val) { return array2.indexOf(val) >= 0; })); // true
 console.log(targetArray.every(function(val) { return array3.indexOf(val) >= 0; }));// false

1
投票

reduce 也可以在这里使用(但它的难度为 O = (N * M)):

const result = target.reduce((acc, el) => {
    return acc && array.includes(el)
}, true);

以更有效的方式解决这个问题(O = N + M):

const myMap = new Map();

array.forEach(element => myMap.set(element);

const result = target.reduce((acc, el) => {
   return acc && myMap.has(el)
}, true);

0
投票

如果您要检查数组 x 是否包含数组 y 中的所有内容,包括要求 y 中多次出现的元素在 x 中多次出现:

function arrayContains(x,y) {
  // returns true if array x contains all elements in array y
  return !x.reduce((y,e,t)=>
    (t=y.indexOf(e),t>=0&&y.splice(t,1),y),[...y]).length
}

console.log(arrayContains([1,2,3],   [1,5]))    // false - no 5 present
console.log(arrayContains([1,2,3],   [1,2]))    // true
console.log(arrayContains([1,2,3],   [1,2,2]))  // false - not enough 2s
console.log(arrayContains([2,1,2,3], [2,2,1]))  // true


0
投票

您可以计算两个数组的差异,如果目标变为空,则意味着找到了所有元素;)

const target = [ 1, 2, 3,    ];
const array1 = [ 1, 2, 3,    ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2,       ]; // false

const containsAll = (target, array) => {
  const difference = target.filter(x => !array.includes(x))
  return difference.length === 0
};

console.assert(containsAll(target, array1), "should be true");
console.assert(containsAll(target, array2), "should be true");
console.assert(containsAll(target, array3) === false, "should be false");


0
投票

const target = [1, 2, 3];
const array1 = [1, 2, 3];
const array2 = [1, 2, 3, 4, ]; // true
const array3 = [1, 2, ];

function check(targetarr, arr) {
  return targetarr.map((e) => {
    return (arr.includes(e)) ? true : false;
  }).every((e) => e === true);
}
console.log(check(target, array1));
console.log(check(target, array2));
console.log(check(target, array3));


0
投票

就代码而言,这种方法可能是有效的。 如果性能是优先考虑的并且对于大型数组,您可能会考虑首先对数组进行排序,然后使用更有效的方法来执行此操作。

抱歉,我刚刚发现这篇文章并没有阅读所有答案,所以如果已经提到了这种方法,请原谅我。

const target = [ 1, 2, 3,    ];
const array1 = [ 1, 2, 3,    ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2,       ]; // false
const array4 = [20,1,6,2,10,3]; // true

function containsAllElmnts(array, target) {
  return target.every(element => array.includes(element));
}

console.log("reference (target) => " + target);

console.log(array1 + " => " + (containsAllElmnts(array1, target)));
console.log(array2 + " => " + (containsAllElmnts(array2, target)));
console.log(array3 + " => " + (containsAllElmnts(array3, target)));
console.log(array4 + " => " + (containsAllElmnts(array4, target)));


0
投票

TC39 指定了许多要添加到 ECMAScript 的 其他

Set
方法,目前计划在 ECMAScript 2025 中添加。

大多数主流浏览器已经支持

isSubsetOf()
方法。这样你就可以做到:

const target = [1, 2, 3];    
const array1 = [1, 2, 3];
const array2 = [1, 2, 3, 4];
const array3 = [1, 2];

console.log(new Set(target).isSubsetOf(new Set(array1))); // true
console.log(new Set(target).isSubsetOf(new Set(array2))); // true
console.log(new Set(target).isSubsetOf(new Set(array3))); // false

当然,您希望消除在实际实现中多次创建

target
集的开销。

如果当前的浏览器支持不足以满足您的需求,您始终可以依赖shim/polyfill

© www.soinside.com 2019 - 2024. All rights reserved.