如何使用 javascript 将两个数组的子集放入不同的数组?

How to get subset of two arrays into a different array using javascript?

我有 2 个数组。

 arr1=[1,8,1,3,2]

 arr2=[3,8,1]

我想将元素 [8,1] 的子集放入 arr3。我如何使用 javascript 执行此操作?我使用了以下代码。但似乎没有用。

function subsetFind() {      
    var arr1 = [1,8,1,3,2]
    var arr2 = [3,8,1]
    var arr3 = [];
    var arr1length = arr1.length;
    var arra2length = arr2.length;

   for(var i = 0; i < arr1length; ++i){
        for(var j=0;j<arra2length;j++) {
            if(arr1[i] != arr2[j]) {
                break;
            } else {
                arr3.push(arr1[i]);
                break;
            }
        }
    }

    alert(arr3);
}

试试这个解决方案 - 它检查当前值和上一个值或当前值和下一个值是否相等:

function subsetFind() {
    var arr1 = [1,8,1,3,2]
    var arr2 = [3,8,1]
    var arr3 = [];
    var arr1length = arr1.length;
    var arra2length = arr2.length;
    var used_i = 0;
    for(var i = 0; i < arr1length; ++i){
        if(used_i != 0 && used_i < i-1){
            break;
        }
        for(var j=0;j<arra2length;j++) {
            if((arr1[i] == arr2[j] && arr1[i-1] == arr2[j-1]) || (arr1[i] == arr2[j] && arr1[i+1] == arr2[j+1])) {
               arr3.push(arr1[i]);
               used_i = i;
            }
        }
    }

    alert(arr3);
}

输出:

8,1

希望你喜欢; (更新:)

                function subset() {
                    var arr1 = [1, 9, 3, 5, 4, 8, 2, 6, 3, 4]
                    var arr2 = [5, 2, 4, 8, 2, 6, 4]
                    var arr3 = [];

                    var minSize=2; // minimum 2 element must in intersection
                    var findedMax = "";

                    var arr1Joined = ""; arr1.forEach(function (a) { arr1Joined += "," + a; });
                    
                    for(k=minSize;k<arr2.length;k++)
                        arr2.forEach(function (x,y) {
                            var fkey="";
                            for (i = y; i <= y+k; i++)
                                fkey += "," + arr2[i];
                            if (arr1Joined.indexOf(fkey) >= 0) findedMax = fkey;
                    });
                    arr3=findedMax.substr(1).split(",");
                    alert(arr3);
                }

试一试:

参考n-dru的回答:

function subset () {
        var arr1 = [1,9,3,5,4,8,2,6,3,4]
        var arr2 = [5,2,4,8,2,6,4]
        var arr3 = [];
        var arr1length = arr1.length;
        var arra2length = arr2.length;
        var finalResult;
       for(var i = 0; i < arr1length; ++i){
            for(var j=0;j<arra2length;j++) {
                if((arr1[i] == arr2[j] && arr1[i-1] == arr2[j-1]) || (arr1[i] == arr2[j] && arr1[i+1] == arr2[j+1])) {
                    arr3.push(arr1[i]);

                }
                else
                {
                    finalResult = arr3.toString();
                }
            }
        }

        alert(finalResult);
}

DEMO

我相信您的问题已在 Simplest code for array intersection in javascript and Finding matches between multiple JavaScript Arrays 中得到解答。您还可以查看 lodash 库中 _.intersection 的实现。

ES6 方式。

[...new Set(arr1)].filter(v => arr2.includes(v))

细分:

new Set(arr1)                   // convert arr1 to Set to remove duplicates 

[...new Set(arr1)]              // convert back to array

arr2.includes(v)                // test if arr2 includes `v`

[...new Set(arr1)].filter(v => arr2.includes(v))  // choose unique elements in both arrays