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

A brief introduction to the method of removing duplicate items from an array in JS

怪我咯
Release: 2017-07-07 15:07:35
Original
1098 people have browsed it

This article mainly introduces the method of JS to simply remove duplicate items in array, involving javascript traversal, judgment and operation related operation skills for array , Friends who need it can refer to

The example in this article describes a simple JS method to remove duplicate items in an array. Share it with everyone for your reference, the details are as follows:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var arr = ["aaa","bbb","aaa","ccc","ddd","ccc"];
function unique(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;
}
console.info(unique(arr));
</script>
</body>
</html>
Copy after login

The renderings are as follows:

The above is the detailed content of A brief introduction to the method of removing duplicate items from an array in JS. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!