Home  >  Article  >  Web Front-end  >  A brief introduction to the method of removing duplicate items from an array in JS

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

怪我咯
怪我咯Original
2017-07-07 15:07:351137browse

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>

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!

Statement:
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