var arr = [1,2,2,3,5,4,8,6,4,2,47,5,9,3,20,3];
//将数组转为对象,将key入对象,对象中key值不允许重复
var toNewObject = function(arr){
var obj = {};
var len= arr.length;
for(var i = 0;i<len;i++){
obj[arr[i]] = true;
}
return obj;
}
//对象转成数组
var toNewArr = function(obj){
var arr =[];
for(var attr in obj){
if(obj.hasOwnProperty(attr)){
arr.push(attr);
}
}
return arr;
}
//通过函数输出筛迭后的数组
var unique = function(arr){
var obj = toNewObject(arr);
var newArr = toNewArr(obj);
return newArr;
}
console.log(unique(arr));
网友评论