<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function unique(arr){
// 遍历arr,把元素分别放入tmp数组(不存在才放)
var tmp = new Array()
for(var i in arr){
//该元素在tmp内部不存在才允许追加
if(tmp.indexOf(arr[i])==-1){
tmp.push(arr[i])
}
}
return tmp
}
alert(unique(['qiang','ming','tao','li','liang','you','qiang','tao']))
</script>
</head>
<body>
</body>
</html>
var obj = {}var arr = [1, 1, 2, 3]
for (var i = 0 i < arr.length i++) {
obj[arr[i]] = true
}
var result = []
for (e in obj) {
result.push(e)
}
console.log(result)//[1,2,3]
把数组中的值保存为对象的键,然后取出对象的键