jQuery.unique

« Обратно к странице инструментов

jQuery.unique( массив )

Удаляет дубликаты из массива элементов. Обратите внимание, этот метод работает только с массивами элементов DOM.
Аргументы:

массив Массив
Массив элементов DOM.
Примеры:

Удаляет дубликаты из массива элементов div.

    var divs = $("div").get();

    // add 3 elements of class dup too (they are divs)
    divs = divs.concat($(".dup").get());
    $("div:eq(1)").text("Pre-unique there are " + divs.length + " elements.");

    divs = jQuery.unique(divs);
    $("div:eq(2)").text("Post-unique there are " + divs.length + " elements.")
                  .css("color", "red");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){

    var divs = $("div").get();

    // add 3 elements of class dup too (they are divs)
    divs = divs.concat($(".dup").get());
    $("div:eq(1)").text("Pre-unique there are " + divs.length + " elements.");

    divs = jQuery.unique(divs);
    $("div:eq(2)").text("Post-unique there are " + divs.length + " elements.")
                  .css("color", "red");

  });
  </script>

  <style>
  div { color:blue; }
  </style>
</head>
<body>
  <div>There are 6 divs in this document.</div>
  <div></div>

  <div class="dup"></div>
  <div class="dup"></div>
  <div class="dup"></div>
  <div></div>

</body>
</html>
Удаляет дубликаты из массива элементов div.
$.unique(document.getElementsByTagName("div"));
[<div>, <div>, ...]
Name Type