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)
使用非常简单的集合,
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)
保留 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.from将 Iterable 作为参数,这将从原始数组创建一个新数组。[...arrays]将数组(参数)展开成一个新的、单个的(所以它变成了一个数组数组)->[5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]变成:[[5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]].flat展平数组,使数组值而不是值数组的 ar 数组 -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat ->[[5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]]变为[5, 10, 15, 15, 88, 1, 5, 7, 100, 15, 10, 1, 5]new Set从数组中删除重复项并返回一个 Iterable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set。->[5, 10, 15, 15, 88, 1, 5, 7, 100, 15, 10, 1, 5]成为没有重复项的 Set 实例(一个 Iterable)。Array.from 然后将 Set (Iterable) 转换为常规数组。更多信息在这里:如何将集合转换为数组?当心: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)
说明(仅与上述不同):
Array.concat我们的原始数组,以便它会展平它,传递一个新数组作为它的this并提供我们的数组作为参数:[].concat.apply([],[...arrays])片段:http : //jsfiddle.net/briosheje/y03osape/2/
没有的片段.flat:http : //jsfiddle.net/briosheje/y03osape/4/