Home > Web Front-end > JS Tutorial > body text

Remove duplicate elements from array_javascript tips

WBOY
Release: 2016-05-16 19:05:52
Original
755 people have browsed it

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> Array.prototype.distinct = function(){ var $ = this; var o1 = {}; var o2 = {}; var o3 = []; var o; for(var i=0;o = $[i];i++){ if(o in o1){ if(!(o in o2)) o2[o] = o; delete $[i]; }else{ o1[o] = o; } } $.length = 0; for(o in o1){ $.push(o); } for(o in o2){ o3.push(o); } return o3; } var a = [2,2,2,3,3,3,4,4,5,6,7,7]; alert("原数组:" + a); alert("有重复的元素是:" + a.distinct()); alert("整理后的数组是:" + a); alert("整理后的长度是:" + a.length) </script>
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