function mergerArr(arr1,arr2){ var flag; for(var i = 0;i<arr1.length;i++){ flag = false; for(var j=0;j<arr2.length;j++){ if(arr1[i] == arr2[j]){ flag = true; break; } } if(!flag){ arr2.push(arr1[i]); } } return arr2; } var a = [1,2,3,4]; var b = [3,4,5,6]; var newArr = mergerArr(a,b); console.log(newArr) 输出 [3,4,5,6,1,2];
JS数组去重的最优方法
function unique(arr) { var result = [], hash = {}; for (var i = 0, elem; (elem = arr[i]) != null; i++) { if (!hash[elem]) { result.push(elem); hash[elem] = true; console.log(hash); } } return result; } var arr = [1,2,3,3,5,4,4,6]; unique(arr);