javascript - 三个数组之间的联合

标签 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]

最佳答案

只是保留 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]));

或者,甚至更短(但阅读起来不太友好):

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

解释:

注意:Array.flat 目前是一项实验性功能 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)。下面不使用 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]));

解释(仅与上述不同):

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

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

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

关于javascript - 三个数组之间的联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53794570/

相关文章:

javascript - 使用 WebStorm 文件夹中的闭包缩小所有 js 文件

javascript - 如何向 go.js 节点添加属性和重定向

Javascript - 附加行相关的下拉列表

python - 处理 jax numpy 数组中的不同形状(jit 兼容)

jquery - 使用函数

javascript - 如何检测数组中的元素是否为一个字符长并且与另一个数组中的任何元素的第一个字符匹配

javascript - Casperjs Google 登录无法正常工作

python - 为什么这个二进制文件的大小是相等的,尽管它们不应该相等?

c++ - 数组原语是C++吗?

c - 在 main 以外的函数中定义的变量的生命周期