三个数组之间的联合

leo*_*fed 2 javascript arrays function

我需要找到传递给函数的三个数组的并集union

我花了大约 50 行代码来获得预期的结果。显然,以下代码有效,但现在我想知道完成相同工作的最佳方法是什么(以功能性和非功能性方式)。

function union(...arrays) {
    var array1 = arguments[0];
    var array2 = arguments[1];
    var array3 = arguments[2];      

    var unique = [];
    var intersaction = [];

    // find the unique values

    for(let i = 0; i < array1.length; i++) {
        if( (array2.includes(array1[i]) == false) && (array3.includes(array1[i])) == false ) {
            unique.push(array1[i]); 
        }
    }

    for(let i = 0; i < array2.length; i++) {
        if( (array1.includes(array2[i]) == false) && (array3.includes(array2[i])) == false ) {
            unique.push(array2[i]); 
        }
    }

    for(let i = 0; i < array3.length; i++) {
        if( (array1.includes(array3[i]) == false) && (array2.includes(array3[i])) == false ) {
            unique.push(array3[i]);
        }
    }

    // find the intersection

    for(let j = 0; j < array1.length; j++) {
        if(array2.includes(array1[j]) || array3.includes(array1[j]) ) {
            if (intersaction.indexOf(array1[j]) == -1) { 
                intersaction.push(array1[j]);
            }
        }
    }

    for(let j = 0; j < array2.length; j++) {
        if(array1.includes(array2[j]) || array3.includes(array2[j]) ) {
            if (intersaction.indexOf(array2[j]) == -1) { 
                    intersaction.push(array2[j]);
            }       
        }
    }

    for(let j = 0; j < array3.length; j++) {
        if(array1.includes(array3[j]) || array2.includes(array3[j]) ) {
            if (intersaction.indexOf(array3[j]) == -1) { 
                    intersaction.push(array3[j]);
            }       
        }
    }

    return union = [...intersaction, ...unique];

}

console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]));
// should log: [5, 10, 15, 88, 1, 7, 100]
Run Code Online (Sandbox Code Playgroud)

Jus*_*ode 5

使用非常简单的集合,

Set 对象允许您存储任何类型的唯一值,无论是原始值还是对象

var a=  [5, 10, 15];
var b=[15, 88, 1, 5, 7];
var c=[100, 15, 10, 1, 5];
var result= [...new Set([...a, ...b,...c])];
console.log(result);
Run Code Online (Sandbox Code Playgroud)


bri*_*eje 5

保留 OP 提供的原始函数签名的另一种解决方案:

function union(...arrays) {
    return Array.from(new Set([...arrays].flat()));
}

console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]));
Run Code Online (Sandbox Code Playgroud)

或者,甚至更短(但不太友好):

return [...(new Set([...arrays].flat()))];

解释:

当心:Array.flat目前是一个实验性功能(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)。下面不使用平面的解决方案:

function union(...arrays) {
    return Array.from(new Set([].concat.apply([],[...arrays])));
}

console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]));
Run Code Online (Sandbox Code Playgroud)

说明(仅与上述不同):

  • 而不是 .flat,我们应用到Array.concat我们的原始数组,以便它会展平它,传递一个新数组作为它的this并提供我们的数组作为参数:[].concat.apply([],[...arrays])

片段:http : //jsfiddle.net/briosheje/y03osape/2/

没有的片段.flathttp : //jsfiddle.net/briosheje/y03osape/4/