function unique(arr) { var result = [], hash = {}; for (var i = 0, elem; (elem = arr[i]) != null; i++) {//循环arr数组 if (!hash[elem]) {//hash数组作用:判断当前元素是否存在 result.push(elem);//不存在:将当前元素存入到result数组中 hash[elem] = true;//设置当前元素存在 } } console.log(JSON.stringify(hash,null,2));//{"f": true,"a": true,"b": true,"c": true,"d": true,"e": true} return result;//返回去重后元素 } var arr = ["f","a","b","c","a","a","b","d","e","e","f"]; var result = unique(arr); console.log(result);//["f", "a", "b", "c", "d", "e"]