判断是不是重复,用的是Object的key,要注意的是数字转key会变成字符串。
var array = [0,0,0,1,2,3,"1",undefined,undefined]; var len = array.length; var result = []; var keys = {}; for(var index = 0;index < len;index ++){ var temp = array[index]; var type = typeof temp; if(!keys[temp]){ keys[temp] = [type]; result.push(temp); }else if(keys[temp].indexOf(type) == -1){ //"1"跟1的key会冲突,都是"1",所以要根据类型进行判断 keys[temp].push(type); result.push(temp); } }
输出:0,1,2,3,1, 最后一位字符串是undefined