且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript:设置数据结构:相交

更新时间:2022-05-07 09:32:25

要获得交集,您可以迭代集合中的项目并检查它们是否属于到另一个:

To get the intersection, you can iterate the items of a set and check if they belong to the other one:

var intersect = new Set();
for(var x of mySet1) if(mySet2.has(x)) intersect.add(x);

在ES7中,您可以使用 array comprehensions generator comprehensions

In ES7 you can simplify it with array comprehensions or generator comprehensions:

var intersect = new Set((for (x of mySet1) if (mySet2.has(x)) x));