简体   繁体   English

如何在JavaScript中检查一个数组是否包含另一个数组的所有元素,包括count?

[英]How can I check it an array contains all elements from another array, including count, in JavaScript?

I have two arrays: 我有两个数组:

arr1 = [1, 2, 3, 1, 2, 3, 4]
arr2 = [1, 3, 1, 1]
arr3 = [1, 1, 2, 2, 3]

Using arr1 as the baseline, arr2 does not match because it contains three 1 's whereas arr1 only has two. 使用arr1作为基线, arr2不匹配,因为它包含三个1 ,而arr1只有两个。 arr3 , however should return true because it has elements from arr1 . arr3 ,但是应返回true因为它具有arr1元素。

I tried 我试过了

if(_.difference(arr2, arr1).length === 0)

But this does not take into account the number of occurrences 但这没有考虑到发生的次数

You could count all value from the first array and iterate the second with every and return early if a value is not given or zero, otherwise decrement the count and go on. 您可以对第一个数组中的所有值进行计数,然后对every数组进行迭代,如果未给定值或为零,则尽早返回,否则递减计数并继续。

 function check(a, b) { var count = a.reduce((o, v) => (o[v] = (o[v] || 0) + 1, o), {}); return b.every(v => count[v] && count[v]--); } var arr1 = [1, 2, 3, 1, 2, 3, 4], arr2 = [1, 3, 1, 1], arr3 = [1, 1, 2, 2, 3]; console.log(check(arr1, arr2)); // false console.log(check(arr1, arr3)); // true 

You can try to loop through second array and compare t against main array if value found make main array cell to false and set flag as true 您可以尝试遍历第二个数组并将t与主数组进行比较(如果找到的值使主数组单元格为false并将标志设置为true)

 arr1 = [1, 2, 3, 1, 2, 3, 4] arr2 = [1, 3, 1, 1] arr3 = [1, 1, 2, 2, 3] function checkArray(compareMe, toThis){ var flag = false; for(var i = 0; i < toThis.length; i++){ flag = false; for(var j = 0; j < compareMe.length; j++){ if(compareMe[j] == toThis[i]){ compareMe[j] = false; flag = true; break; } } } return flag; } console.log(checkArray(arr1, arr2)); console.log(checkArray(arr1, arr3)); 

Try this solution: 试试这个解决方案:

 const arr1 = [1, 2, 3, 1, 2, 3, 4], arr2 = [1, 3, 1, 1], arr3 = [1, 1, 2, 2, 3], arr4 = [1, 2, 3, 1, 2, 3, 4, 1]; function containsFn(src, trg) { const srcCp = src.slice(); for(let i = 0; i < trg.length; i++) { const index = srcCp.indexOf(trg[i]); if(index > - 1) { srcCp.splice(index, 1); } else { return false; } } return true; } console.log(containsFn(arr1, arr2)); console.log(containsFn(arr1, arr3)); console.log(containsFn(arr1, arr4)); 

Looks like I'm already late to the party but this would be a recursive solution: 看来我参加聚会已经很晚了,但这将是一个递归解决方案:

 arr1 = [1, 2, 3, 1, 2, 3, 4] arr2 = [1, 3, 1, 1] arr3 = [1, 1, 2, 2, 3] function findAllIn(find, search) { if (find.length == 0) { return true; } i = search.indexOf(find[0]); if (i == -1) { return false; } return findAllIn(find.slice(1,find.length), search.slice(0,i).concat(search.slice(i+1, search.length))); } console.log(findAllIn(arr2, arr1)); // false console.log(findAllIn(arr3, arr1)); // true 

This should do the trick 这应该可以解决问题

Not efficient but I think it is easy to understand 效率不高,但我认为这很容易理解

 const count = (x, xs) => xs.reduce((y, xi) => y + (xi === x ? 1 : 0), 0) const isInclusive = (xs, ys) => xs.every((xi) => count(xi, xs) >= count(xi, ys)) const arr1 = [1, 2, 3, 1, 2, 3, 4] const arr2 = [1, 3, 1, 1] const arr3 = [1, 1, 2, 2, 3] console.log(isInclusive(arr1, arr2)) console.log(isInclusive(arr1, arr3)) 

Based on this answer: https://stackoverflow.com/a/4026828/3838031 基于此答案: https : //stackoverflow.com/a/4026828/3838031

You can do this: 你可以这样做:

 Array.prototype.diff = function(a) { return this.filter(function(i) {return a.indexOf(i) < 0;}); }; arr1 = [1, 2, 3, 1, 2, 3, 4] arr2 = [1, 3, 1, 1] arr3 = [1, 1, 2, 2, 3] console.log((arr1.diff(arr2).length === 0) ? "True" : "False"); console.log((arr1.diff(arr3).length === 0) ? "True" : "False"); console.log((arr2.diff(arr1).length === 0) ? "True" : "False"); console.log((arr2.diff(arr3).length === 0) ? "True" : "False"); console.log((arr3.diff(arr1).length === 0) ? "True" : "False"); console.log((arr3.diff(arr2).length === 0) ? "True" : "False"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 Javascript 数组是否包含另一个数组的所有其他元素 - How do I check if a Javascript array contains all the other elements of another array 如何检查一个数组是否包含另一个数组的所有元素 - How to check if an array contains all the elements of another another array 检查数组是否包含另一个数组的所有元素 - Check if array contains all elements of another array 如何检查子数组是否包含另一个数组 javascript 的所有元素 - How to check if a sub-array contains all elements of another array javascript 检查一个数组是否包含另一个数组的所有元素,包括重复是否出现两次 - Check to see if an array contains all elements of another array, including whether duplicates appear twice 检查一个数组是否包含来自另一个数组的元素 - Check if an array contains elements from another array 如何检查字符串是否包含 Javascript 中数组的所有元素 - How to check if a string contains all the elements of an array in Javascript Javascript:如何检查数组是否包含另一个数组? - Javascript: How do I check if an array contains exactly another array? 如何检查数组是否在javascript中包含另一个数组 - how to check if array contains an another array in javascript 检查一个javascript数组是否包含另一个数组的所有元素或元素值的一部分 - Check if a javascript array contains all elements or part of element values of another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM