如果你現在正在面試請保持冷靜的頭腦,從容應對;因為頭腦一熱可能該會的也不會了,下面貼出來一段簡單的陣列去重複的方法,寫的有點簡單僅供大家參考,如果不足之處請多賜教;
var ary = [1,2,3,2,1,4,4,423,323,"1","323"];
function getSingleArray(ary){
var obj = {},
newArray = [];
if(typeof ary === "object"){
for(var i=0; i<ary.length; i++){
var cur = ary[i];
if(!!obj[cur] && typeof obj[cur]==typeof cur){
continue;
}else{
obj[cur] = cur;
newArray.push(obj[cur]);
}
}
}
return newArray;
}
var b = getSingleArray(ary);
console.log("新陣列:",b);
console.log(ary);
只是簡單的判斷,後續會越來越完善判斷陣列的型別