方法一:
var arr2=[]; for(var i=0;i<arr1.length;i++){ if(arr2.indexOf(arr1[i])==-1){ arr2.push(arr1[i]) } } console.log(arr2);
方法二:
var arr1 = [1, 2, 2, 3, 4, 4, 4, 4]; var arr2 = []; for (var i = 0; i < arr1.length; i++) { for (var j = 0; j < arr1.length; j++) { if (arr2.includes(arr1[i]) == false) { arr2.push(arr1[i]) } } } console.log(arr2);
方法三:
var arr1 = [1, 2, 2, 3, 4, 4, 4, 4]; var arr2 = []; for (var i = 0; i < arr1.length; i++) { for (var j = 0; j < arr1.length; j++) { if (arr2.includes(arr1[i]) == false) { arr2.splice(0, 0, arr1[i]) } } } console.log(arr2);