简体   繁体   English

两个 arrays 在多个 arrays 中的交集

[英]Intersection of two arrays in multiple arrays

I'm searching for a way for extracting common values in at least two arrays in the list of arrays in JS.我正在寻找一种方法来提取 JS 中 arrays 列表中至少两个 arrays 中的公共值。

eg [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]] to ["reddit", "telegram] [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]] ["reddit", "telegram]

The point is 1. the arrays can be any length 2. common at least two (or any specified number) of arrays but not all of the arrays.要点是 1. arrays 可以是任意长度 2. 至少有两个(或任何指定数量)arrays 但不是全部 arrays。

I looked a countless numbers of SO posts and also looked the lodash functions, they are all talking either "of two arrays" or "of all arrays from multiple arrays", none of them were takling "of two/arbitrary arrays from multiple arrays".我查看了无数的 SO 帖子,也查看了 lodash 函数,他们都在谈论“两个数组”或“来自多个数组的所有 arrays”,没有一个是“来自多个数组的两个/任意 arrays” . So I'm here to ask.所以我是来问的。

Thanks.谢谢。

 let arr = [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]] const flatedArr =arr.flat(Infinity); const repeatedItems = [] const nonRepeatedItems = [] flatedArr.forEach(val => { if(nonRepeatedItems.includes(val)) return repeatedItems.push(val) return nonRepeatedItems.push(val) }) console.log(repeatedItems); // ["reddit","telegram"]

Here is an O(n) version using Map to not bother about the type of the values这是一个 O(n) 版本,使用 Map 不关心值的类型

Note this will also return dupes in one array请注意,这还将在一个数组中返回重复项

 const extractCommonValues = arr => { const counts = arr.flat().reduce((acc, cur) => (acc.set(cur, (acc.get(cur) || 0) + 1), acc), new Map()); return [...counts.keys()].filter(el => counts.get(el) > 1); }; console.log( extractCommonValues([["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]]) ) // will extract the dupe amazon in the first array console.log( extractCommonValues([["google", "amazon", "reddit", "amazon"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]]) )

To ignore dupes in the sub arrays we can set them first要忽略子 arrays 中的欺骗,我们可以先设置它们

 const extractCommonValues = arr => { const counts = arr.map(subArr => [...new Set(subArr)]).flat().reduce((acc, cur) => (acc.set(cur, (acc.get(cur) || 0) + 1), acc), new Map()); return [...counts.keys()].filter(el => counts.get(el) > 1); }; console.log( extractCommonValues([["google", "amazon", "reddit", "amazon"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]]) )

using various Array methods, and Set , it can be accomplished fairly easily使用各种 Array 方法和Set ,它可以很容易地完成

 const input = [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]]; // remove duplicaates within a sub array const uniqueInSub = input.map(_=>[...new Set(_)]); // create a list of all values in a flat array const uniqueInSubFlat = uniqueInSub.flat(); // list of unique values const values = [...new Set(uniqueInSubFlat)]; // array of [value, count] pairs const pre1 = values.map(value => [ value, uniqueInSubFlat.filter(subValue => subValue === value).length]); // value,count pairs where the count > 1 and < the number of sub arrays const pre2 = pre1.filter(([, count]) => count > 1 && count < input.length); // array of values const result = pre2.map(([value])=>value); console.log(result);

 function findCommonValues(arrays, minCommon) { // Create an object to keep track of the count of each value const countMap = new Map(); // Iterate over each array for (const array of arrays) { // Iterate over each value in the array for (const value of array) { // Increment the count for the current value countMap.set(value, (countMap.get(value) || 0) + 1); } } // Filter the values that have a count equal to or greater than the specified minimum const commonValues = []; for (const [value, count] of countMap.entries()) { if (count >= minCommon) { commonValues.push(value); } } return commonValues; } // Example usage const arrays = [ ["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"] ]; const commonValues = findCommonValues(arrays, 2); console.log(commonValues); // Output: ["reddit", "telegram"]

You could use .flat() and .filter() methods as follows:您可以使用.flat().filter()方法,如下所示:

 const data = [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]], common = [...new Set(data.flat().filter((v,i,a) => a.filter(val => val === v).length > 1))]; console.log( common );

Or More efficiently....或者更有效....

 const data = [["google", "amazon", "reddit"], ["telegram", "reddit", "discord"], ["firefox", "telegram", "chrome"]], all = data.flat(), uniq = [...new Set(all)], common = uniq.filter(v => all.filter(val => val === v).length > 1); console.log( common );

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

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