[英]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
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.