简体   繁体   English

比较数组中的数组

[英]Comparing arrays in arrays

I'm looking for a way to make comparisons between arrays in arrays. 我正在寻找一种在数组中的数组之间进行比较的方法。

let small = [[1, 3], [2, 2], [2, 3], [3, 0]];

let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]];

For example, I would like to be able to find out how many of the arrays in small are found in large . 例如,我希望能够找出在large找到了多少个small数组。 Some function that, given the arrays above as arguments, would return 2 , since [2, 2] and [3, 0] from small are found in large . 给定上述数组作为参数的某些函数将返回2 ,因为在large中找到了来自small [3, 0] [2, 2][3, 0]

How would you go about doing that? 您将如何去做?

You can convert one of the arrays into a Set of hashes, and than filter the 2nd array using the set: 您可以将其中一个数组转换为一哈希,然后使用该集合过滤第二个数组:

 const small = [[1, 3], [2, 2], [2, 3], [3, 0]]; const large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]]; const containsCount = (arr1, arr2, hashFn) => { const arr1Hash = new Set(arr1.map(hashFn)); return arr2.filter(s => arr1Hash.has(hashFn(s))).length; } const result = containsCount(small, large, ([a, b]) => `${a}-${b}`); console.log(result); 

You can try something like: 您可以尝试类似:

 let small = [[1, 3], [2, 2], [2, 3], [3, 0]]; let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]]; let z = zeta(small, large); console.log(z); function zeta(a, b) { let join = m => m.join(); let x = a.map(join); let y = b.map(join); return x.reduce((n, m) => (y.indexOf(m)>0) ? ++n : n, 0); } 

I hope this helps! 我希望这有帮助!

Use every and some to compare the arrays with each other. 使用everysome比较彼此的阵列。

If you want to get an array containing the subarrays that match, use filter : 如果要获取包含匹配的子数组的数组,请使用filter

let result = small.filter(arr => 
  large.some(otherArr =>
    otherArr.length === arr.length && otherArr.every((item, i) => item === arr[i])
  )
);

Which filters the subarray from small that some subarray from large has the same length and the same elements/items. 过滤从子阵列small ,从一些子阵large具有相同的length和相同的元素/项。

Demo: 演示:

 let small = [[1, 3], [2, 2], [2, 3], [3, 0]]; let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]]; let result = small.filter(arr => large.some(otherArr => otherArr.length === arr.length && otherArr.every((item, i) => item === arr[i]) ) ); console.log(result); 

And if you want just a count, then use reduce instead of filter to count the mathched items (this makes use of the fact that the numeric value of true is 1 and that of false is 0 ): 如果只想计数,则使用reduce而不是filter来计算被算出的项目(这利用了true值为1以及false值为0的事实):

let count = small.reduce((counter, arr) => 
  counter + large.some(otherArr =>
    otherArr.length === arr.length && otherArr.every((item, i) => item === arr[i])
  )
, 0);

Demo: 演示:

 let small = [[1, 3], [2, 2], [2, 3], [3, 0]]; let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]]; let count = small.reduce((counter, arr) => counter + large.some(otherArr => otherArr.length === arr.length && otherArr.every((item, i) => item === arr[i]) ) , 0); console.log(count); 

Note: If the subarrays contain only numbers, the code could be simplified to use Array#toString instead of every and length comparaison: 注意:如果子Array#toString仅包含数字,则可以将代码简化为使用Array#toString而不是every length

let result = small.filter(arr => large.some(otherArr => "" + otherArr === "" + arr));

Which casts both arrays into strings and compares the two strings instead. 它将两个数组都转换为字符串,然后比较两个字符串。 This can be used with the reduce as well. 这也可以与reduce一起使用。

Create two nesting map() function oute for small and inner for large then use JSON.stringify() 为小文件创建两个嵌套map()函数,为大文件创建内部,然后使用JSON.stringify()

let small = [[1, 3], [2, 2], [2, 3], [3, 0]];
let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]];
var same=[];
    small.map(function(element){
        large.map(function(element2){
            if(JSON.stringify(element)==JSON.stringify(element2)){
        same.push(element);
  }
 });
});
console.log(same);

 let small = [[1, 3], [2, 2], [2, 3], [3, 0]]; let large = [[1, 0], [1, 1], [2, 0], [2, 2], [2, 5], [3, 0], [3, 2]]; let largeArrayStringForm = large.map(item => item.toString()) let matchingItems = small.filter(item => largeArrayStringForm.includes(item.toString())) console.log(`matchCount: ${matchingItems.length}`) 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM