我的用例的 JS 列表交集

发布于 2024-12-01 15:33:45 字数 790 浏览 2 评论 0原文

我是一个 javascript 新手,所以请耐心等待。我的列表如下:

var list1 = ['a','b','c'];
var list2 = ['c','d','e'];
var list3 = ['f','g'];

如您所见,list1 和 list2 在“c”处相交,而 list3 与 list1 和 list2 都不相交。
结果应该是

['a','b','c','d','e'],['f','g'] // Two arrays

我们合并了 list1 和 list2,因为它们相交,同时保留 list3 不变。另一个例子:

var list1 = ['a','b','c'];
var list2 = ['d','e','f'];
var list3 = ['f','g','a'];

这里我们看到list1和list2不相交,list1与list3在“a”处相交,list2与list3在“f”处相交。因此,由于所有 3 个相交,返回的结果将是:

['a','b','c','d','e','f','g'] // One array

任何帮助表示赞赏
KA

PS:我确实在网站上搜索了类似的问题,并且遇到了一个 n 的交集通过 JS 列出 它类似,但不适合我的用例。

I am a javascript newbie so bear with me. I have lists as so:

var list1 = ['a','b','c'];
var list2 = ['c','d','e'];
var list3 = ['f','g'];

As you see, list1 and list2 intersect at 'c' while list3 is disjoint from both list1 and list2.
The result should be

['a','b','c','d','e'],['f','g'] // Two arrays

We have combined list1 and list2 since they intersect while leaving list3 as is. Another example:

var list1 = ['a','b','c'];
var list2 = ['d','e','f'];
var list3 = ['f','g','a'];

Here we see list1 and list2 don't intersect, list1 intersects with list3 at 'a' and list2 intersects list3 at 'f'. So since all 3 intersect, the result returned would be:

['a','b','c','d','e','f','g'] // One array

Any help is appreciated
KA

PS: I did search the site for a similar problem and I came across one intersection of n lists via JS Its similar but doesn't serve my use case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2024-12-08 15:33:45

如果你有两个列表的交集和并集函数,你可以用这个逻辑得到你想要的:

var lists; // initialized with the lists you want to process
var merged = [];
for each list a in lists {
    for each list b in merged {
        if intersect(a,b) != empty {
            remove b from merged;
            a = union(a,b);
        }
    }
    add a to merged;
}

当你循环遍历 merged 的​​所有元素时,你只需要小心地从 merged 中删除 b 。从最后一个元素到第一个元素迭代合并可能会更简单。
下划线库是查找交集和并集函数的好地方。

If you have two-list intersection and union functions, you can get what you want with this logic:

var lists; // initialized with the lists you want to process
var merged = [];
for each list a in lists {
    for each list b in merged {
        if intersect(a,b) != empty {
            remove b from merged;
            a = union(a,b);
        }
    }
    add a to merged;
}

You just have to be a little careful about removing b from merged while you are looping through all the elements of merged. It might be simpler to iterate through merged from the last to the first element.
The underscore library is a nice place to find functions for intersection and union.

泪冰清 2024-12-08 15:33:45

尝试

<script>
    Array.prototype.unique = function() {  
        var temp = {}, len = this.length;
        for(var i=0; i < len; i++)  {  
            if(typeof temp[this[i]] == "undefined") {
                temp[this[i]] = 1;
            }  
        }  
        this.length = 0;
        len = 0;
        for(var i in temp) {  
            this[len++] = i;
        }  
        return this;  
    }  

    var list1 = ['a','b','c'];
    var list2 = ['c','d','e'];
    var list3 = ['f','g'];


    start = new Date().getTime(); 
    var list = list1.concat(list2).concat(list3).unique(); 
</script>

try

<script>
    Array.prototype.unique = function() {  
        var temp = {}, len = this.length;
        for(var i=0; i < len; i++)  {  
            if(typeof temp[this[i]] == "undefined") {
                temp[this[i]] = 1;
            }  
        }  
        this.length = 0;
        len = 0;
        for(var i in temp) {  
            this[len++] = i;
        }  
        return this;  
    }  

    var list1 = ['a','b','c'];
    var list2 = ['c','d','e'];
    var list3 = ['f','g'];


    start = new Date().getTime(); 
    var list = list1.concat(list2).concat(list3).unique(); 
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文