把目标数组arr的元素值和键的位置调换 自动就把重复的元素给删除掉了,调换后的样子:array('qiang'=>1,'ming'=>1,'tao'=>1)
<!DOCTYPE html>
<html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body><div id="we">11</div><script type="text/javascript"> var student = [1,2,3,4,5,3,2,1,4,6,7,8,3,3]; document.getElementById('we').innerHTML=unique(student).join(','); function unique(arr){ var tmp = new Array(); for(var m in arr){ tmp[arr[m]]=1; } //再把键和值的位置再次调换 var tmparr = new Array(); for(var n in tmp){ tmparr.push(n); } return tmparr; }</script> </body></html>