Home > Web Front-end > JS Tutorial > An efficient way to remove duplicate items from js arrays

An efficient way to remove duplicate items from js arrays

不言
Release: 2018-04-26 14:35:15
Original
1598 people have browsed it

The Array type does not provide a method to remove duplicates. If you want to remove duplicate elements from the array, you have to find a way yourself. So let me share with you how to efficiently remove duplicates from js arrays. Friends in need can refer to


This is a highly efficient method to remove duplicates:

function hovercUnique(arr) {
var result = [], hash = {};
for (var i = 0, elem; (elem = arr[i]) != null; i++) {
if (!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
//http://hovertree.com
}
Copy after login


Usage example effect:
http://hovertree.com/texiao/jsstudy/4/

http ://hovertree.com/code/dev/lg17a37a.htm

Usage:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>高效率去掉js数组中重复项的特效 - 何问起</title><base target="_blank" />
<meta charset="utf-8" />
<style>a{color:deeppink;}</style>
</head>
<body>
<p><a href="http://hovertree.com/">首页</a> <a href="http://hovertree.com/texiao/">特效</a> <a href="http://hovertree.com/h/bjaf/ovjl4eus.htm">原文</a>
</p>
<p>
<script>
var h_hewenqiArray = new Array();
h_hewenqiArray[0] = "hovertree";
h_hewenqiArray[1] = "easysector";
h_hewenqiArray[2] = "hovertree";
h_hewenqiArray[3] = "keleyi";
h_hewenqiArray[4] = "keleyi";
h_hewenqiArray[5] = "何问起";
h_hewenqiArray[6] = "hovertree";
h_hewenqiArray[7] = "hoverclock";
h_hewenqiArray[8] = "yestop";
h_hewenqiArray[9] = 163;
h_hewenqiArray[10] = "何问起";
h_hewenqiArray[11] = 163;
h_hewenqiArray[12] = "hoverclock";
h_hewenqiArray[13] = "何问起";

var h_arrayLength = h_hewenqiArray.length;
document.write("原始数组元素个数:"+h_arrayLength + "<br />");
for (var i = 0; i < h_arrayLength; i++)
{
document.write(h_hewenqiArray[i]+"<br />");
}


function hovercUnique(arr) {
var result = [], hash = {};
for (var i = 0, elem; (elem = arr[i]) != null; i++) {
if (!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
//http://hovertree.com
}

var h_hewenqiResult = hovercUnique(h_hewenqiArray);

var h_resultLength = h_hewenqiResult.length;
document.write("<br />去重复后数组元素个数:" + h_resultLength + "<br />");
for (var i = 0; i < h_resultLength; i++) {
document.write(h_hewenqiResult[i] + "<br />");
} 
</script>
</p>
<p style="border:solid 1px silver"><p>何问起 hovertree.com</p></p>
</body>
</html>
Copy after login


The above is the detailed content of An efficient way to remove duplicate items from js arrays. For more information, please follow other related articles on the PHP Chinese website!

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