Home > Web Front-end > JS Tutorial > How to remove duplicate elements from an array in javascript [Example]_javascript skills

How to remove duplicate elements from an array in javascript [Example]_javascript skills

WBOY
Release: 2016-05-16 15:05:52
Original
1636 people have browsed it

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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template