js去除数组中重复的一个元素,保留一个元素代码
<scripttypet="text/javascript"src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script><script>$(document).ready(function(){//直接定义结果数组varar
<script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function(){ //直接定义结果数组 var arr = ['Ajil','Ajil','Al Muktatfi Billah Shah','Al Muktatfi Billah Shah','Ayer Puteh','Ayer Puteh','Bukit Besi','Bukit Besi','Bukit Payong','Bukit Payong','Ceneh','Ceneh','Chalok','Chalok','Cukai','Cukai','Dungun','Dungun','Jerteh','Jerteh','Kampung Raja','Kampung Raja','Kemasek','Kemasek','Kerteh','Kerteh','Ketengah Jaya','Ketengah Jaya','Kijal','Kijal','Kuala Berang','Kuala Berang','Kuala Besut','Kuala Besut','Kuala Terengganu','Kuala Terengganu','Marang','Marang','Paka','Paka','Permaisuri','Permaisuri','Sungai Tong','Sungai Tong']; var new_arr=[]; for(var i=0;i<arr.length;i++) { var items=arr[i]; //判断元素是否存在于new_arr中,如果不存在则插入到new_arr的最后 if($.inArray(items,new_arr)==-1) { new_arr.push(items); } } document.write(new_arr); }); </script> |