如果你现在正在面试请保持冷静的头脑,从容应对;因为头脑一热可能该会的也不会了,下面贴出来一段简单的数组去重复的方法,写的有点简单仅供大家参考,如果不足之处请多赐教;
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);
只是简单的判断,后续会越来越完善判断数组的类型