在Javascript中使用.filter()和.map()解决两个集合的交集
我想了解更多关于JS函数.filter()和.map()的信息 ... 所以我试图用它们来解决以下问题: 我有一个包含两个元素的数组-两个逗号分隔的数字列表:在Javascript中使用.filter()和.map()解决两个集合的交集,javascript,Javascript,我想了解更多关于JS函数.filter()和.map()的信息 ... 所以我试图用它们来解决以下问题: 我有一个包含两个元素的数组-两个逗号分隔的数字列表: let testArray = ["2, 3, 4, 7, 14", "1, 2, 4, 7, 18"]; 我想找到这两个集合/元素的交集,并将它们作为数字数组返回。(或者用逗号分隔的字符串也可以) 我当前的代码生成所需的输出,但重复两次,即 [ 2, 4, 7, 2, 4, 7 ] 我还不知道为什么。如有任何帮助,将不胜感激: le
let testArray = ["2, 3, 4, 7, 14", "1, 2, 4, 7, 18"];
我想找到这两个集合/元素的交集,并将它们作为数字数组返回。(或者用逗号分隔的字符串也可以)
我当前的代码生成所需的输出,但重复两次,即
[ 2, 4, 7, 2, 4, 7 ]
我还不知道为什么。如有任何帮助,将不胜感激:
let testArray = ["2, 3, 4, 7, 14", "1, 2, 4, 7, 18"];
function findTheIntersection(array) {
let a = array.join();
a = a.split(',').map(Number); //.map(String);
return a.filter(function(x){
return a.indexOf(x) !== a.lastIndexOf(x)
});
}
console.log(findTheIntersection(testArray));
当前,.indexOf()
将始终引用给定元素的第一个匹配项,因此即使是在给定元素的最后一个匹配项上,使用indexOf()
也将为您提供第一个匹配项的索引(如果它在数组中较早出现)
例如:
0 1 2 3 4 5 6 7 8 9
[2, 3, 4, 7, 14, 1, 2, 4, 7, 18]
如果您在索引0上查看元素2,a.indexOf(2)
将给出0
,a.lastIndexOf(2)
将给出6
。作为0!==6
为真,元素2
保留。现在,如果我们在数组中向上移动,我们最终会到达索引6。如果我们在索引6上查看元素2
,执行a.indexOf(6)
仍将给出0
,a.lastIndexOf(6)
仍将给出6
。由于这些值不相等,您将得到true
,并再次保留2
,作为结果的一部分
相反,您希望使用传入.filter()
的index参数,对照上一个索引检查元素的实际索引:
让testArray=[“2,3,4,7,14”,“1,2,4,7,18”];
函数findTheIntersection(数组){
设a=array.join();
a=a.split(',').map(编号);
返回a.filter(函数(x,i){
返回i!==a.lastIndexOf(x)
});
}
控制台日志(findTheIntersection(testArray))
当前,.indexOf()
将始终引用给定元素的第一个匹配项,因此即使是在给定元素的最后一个匹配项上,如果第一个匹配项在数组中出现较早,则使用indexOf()
将为您提供索引
例如:
0 1 2 3 4 5 6 7 8 9
[2, 3, 4, 7, 14, 1, 2, 4, 7, 18]
如果您在索引0上查看元素2,a.indexOf(2)
将给出0
,a.lastIndexOf(2)
将给出6
。作为0!==6
为真,元素2
保留。现在,如果我们在数组中向上移动,我们最终会到达索引6。如果我们在索引6上查看元素2
,执行a.indexOf(6)
仍将给出0
,a.lastIndexOf(6)
仍将给出6
。由于这些值不相等,您将得到true
,并再次保留2
,作为结果的一部分
相反,您希望使用传入.filter()
的index参数,对照上一个索引检查元素的实际索引:
让testArray=[“2,3,4,7,14”,“1,2,4,7,18”];
函数findTheIntersection(数组){
设a=array.join();
a=a.split(',').map(编号);
返回a.filter(函数(x,i){
返回i!==a.lastIndexOf(x)
});
}
控制台日志(findTheIntersection(testArray))代码>您可以将分割的值作为它们自己数组中的数字,并使用
let array=[“2,3,4,7,14”,“1,2,4,7,18”],
公共=数组
.map(v=>v.split(/,\s*/).map(数字))
.reduce((a,b)=>a.filter(Set.prototype.has,new Set(b));
console.log(通用)代码>您可以将分割的值作为它们自己数组中的数字,并使用
let array=[“2,3,4,7,14”,“1,2,4,7,18”],
公共=数组
.map(v=>v.split(/,\s*/).map(数字))
.reduce((a,b)=>a.filter(Set.prototype.has,new Set(b));
console.log(通用)代码>只需按如下所示使用map reduce即可获得结果:
let testArray = ["2, 3, 4, 7, 14", "1, 2, 4, 7, 18"];
function findTheIntersection(array) {
let a = array.join();
a = a.split(',').map(Number); //.map(String);
var duplicates = a.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);
return duplicates
}
console.log(findTheIntersection(testArray));
让testArray=[“2,3,4,7,14”,“1,2,4,7,18”];
函数findTheIntersection(数组){
设a=array.join();
a=a.split(',').map(数字);//.map(字符串);
var duplicates=a.reduce(功能(acc、el、i、arr){
如果(arr.indexOf(el)!==i和acc.indexOf(el)<0)acc.push(el);返回acc;
}, []);
返回副本
}
控制台日志(findTheIntersection(testArray));
只需按如下所示使用map reduce即可获得结果:
let testArray = ["2, 3, 4, 7, 14", "1, 2, 4, 7, 18"];
function findTheIntersection(array) {
let a = array.join();
a = a.split(',').map(Number); //.map(String);
var duplicates = a.reduce(function(acc, el, i, arr) {
if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
}, []);
return duplicates
}
console.log(findTheIntersection(testArray));
让testArray=[“2,3,4,7,14”,“1,2,4,7,18”];
函数findTheIntersection(数组){
设a=array.join();
a=a.split(',').map(数字);//.map(字符串);
var duplicates=a.reduce(功能(acc、el、i、arr){
如果(arr.indexOf(el)!==i和acc.indexOf(el)<0)acc.push(el);返回acc;
}, []);
返回副本
}
控制台日志(findTheIntersection(testArray));
您的代码找不到交叉点。它在集合的串联中查找重复项。用[“1,2,3”,“1,4,4”]试试,你会看到它发射“4”,这是重复的,但不在交叉点。考虑根据元素是否存在于另一个集合中筛选其中一个集合(当前您正在通过元素是否在该联盟中复制来过滤这两个集合的结合)。安得烈,你说得对-谢谢。我会调查的,你的代码找不到交叉点。它在集合的串联中查找重复项。用[“1,2,3”,“1,4,4”]试试,你会看到它发射“4”,这是重复的,但不在交叉点。考虑根据元素是否存在于另一个集合中筛选其中一个集合(当前您正在通过元素是否在该联盟中复制来过滤这两个集合的结合)。安得烈,你说得对-谢谢。我会调查的。尼克,谢谢你的全面回复。谢谢。@Rain不用担心:)尼克,谢谢你的全面回复。(不用担心:)