我需要一个函数,如果给定的数组包含了一个指定的“目标”数组中的所有元素,则返回true
,否则返回false
。如下所示。
const target = [ 1, 2, 3, ];
const array1 = [ 1, 2, 3, ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2, ]; // false
我该如何实现上述结果?
我需要一个函数,如果给定的数组包含了一个指定的“目标”数组中的所有元素,则返回true
,否则返回false
。如下所示。
const target = [ 1, 2, 3, ];
const array1 = [ 1, 2, 3, ]; // true
const array2 = [ 1, 2, 3, 4, ]; // true
const array3 = [ 1, 2, ]; // false
我该如何实现上述结果?
.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
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);
}
你可以尝试使用 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]));
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]));
这里也可以使用reduce(但它的复杂度为O =(N * M)):
const result = target.reduce((acc, el) => {
return acc && array.includes(el)
}, true);
const myMap = new Map();
array.forEach(element => myMap.set(element);
const result = target.reduce((acc, el) => {
return acc && myMap.has(el)
}, true);
如果您正在使用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
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");
如果您正在检查数组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
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));
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)));