函数式编程
应该符合你的要求: 支持任意多个数组进行合并// 数组合并function mergeArray(){ if (arguments.length === 0) { throw new Error('至少一个参数!'); } if (arguments.length === 1) { return arguments[0]; } /* * 这边采取的累积合并 * 第一次循环 args1 , args2 ===> mergeArr * 第二次循环 mergeArr , args3 === > mergeArr * 第三次循环 mergeArr , args4 ==> mergeArr .... * 最终返回 排序后的 mergeArr */ var superposition = false; for (var i = 0; i < arguments.length; ++i) { if (i + 1 < arguments.length) { if (!superposition) { superposition = arguments[i]; filterArr(superposition); } var two = arguments[i + 1]; filterArr(two); var copyTwo = copyArr(two); var waitMerge = []; for (var n = 0; n < superposition.length; ++n) { var val1 = superposition[n]; var isExists = false; for (var a = 0; a < two.length; ++a) { var val2 = two[a]; if (val1 === val2) { isExists = true; break; } else { isExists = false; } } if (!isExists) { waitMerge.push(val1); } } for (var m = 0; m < waitMerge.length; ++m) { copyTwo.push(waitMerge[m]); } superposition = copyTwo; } } superposition.sort(function(a , b){ return a - b; }); return superposition;}// 数组复制(创建副本)function copyArr(arr){ var newArr = []; for (var i = 0; i < arr.length; ++i) { newArr[i] = arr[i]; } return newArr;}// 数组过滤(影响原数组)function filterArr(arr){ for (var i = 0 ; i < arr.length; ++i) { var idx = false; for (var n = i + 1; n < arr.length; ++n) { if (arr[n] === arr[i]) { idx = n; break; } } if (typeof idx === 'number') { arr.splice(idx , 1); i--; } }}console.log(mergeArray([0 , 1 ,2 , 3 , 4] , [4 , 5 , 8 , 0 , 1] , [10 , 100 , 6]));