In practical applications, we may often need to remove duplicate elements in an array. The following is the implementation of the JavaScript array deduplication method:
<script language="javascript"> <!-- /*判断数组中是否存在某个元素的方法*/ function isExistInArr(_array, _element){ if(!_array || !_element) return false; if(!_array.length){ return (_array == _element); } for(var i=0; i<_array.length; i++){ if(_element == _array[i]) return true; } return false; } /*去除数组中重复元素的方法*/ function distinct(_array){ if(!_array || !_array.length) return _array; var newArray = new Array(); for(var i=0; i<_array.length; i++){ var oEl = _array[i]; if(!oEl || this.isExistInArr(newArray, oEl)) continue; newArray[newArray.length] = oEl; } return newArray; } var origArr = [1,2,3,4,1,4,1,3]; origArr = distinct(origArr); alert(origArr); //--> </script>
The above implementation method [example] of removing duplicate elements from arrays in javascript is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.