For example: var student = ['qiang','ming','tao','li','liang','you','qiang','tao'];
The first idea is: traverse the array arr to be deleted, put the elements into another array tmp, and only allow them to be put into tmp after judging that the element does not exist in arr
Use two functions: for ...in and indexOf()
<script type="text/javascript"> var student = ['qiang','ming','tao','li','liang','you','qiang','tao'];function unique(arr){//Transfer arr and put elements into the tmp array separately (it does not exist) var tmp = new Array();for(var i in arr){//After appending if(tmp.indexOf(arr[i])==-1){tmp.push(arr[i]);}}return tmp;}</script>The second idea is: automatically delete the duplicate elements by changing the element value of the target array arr and the position of the key. The replacement looks like: array('qiang'=>1,'ming'=>1,'tao'=>1)
<script type="text/javascript">var student = ['qiang','ming','tao','li','liang','you','qiang','tao'];function unique(arr){var tmp = new Array();for(var m in arr){tmp[arr[m]]=1;}//Switch the key and value positions again var tmparr = new Array();for(var n in tmp){tmparr.push(n);}return tmparr;}</script>