Merge array and get the unique : merge « jQuery « JavaScript Tutorial






<html>
  <head>
    <script type='text/javascript' src='js/jquery-1.3.2.js'></script>
    <script type='text/javascript'>
$(document).ready(
  function() {
    var $p = $('p').get();
    
    var $before = "Before:\n";
    
    $($p).each(
      function() {
        $before += this.id + "\n";
      }
    );

    $p = $p.concat(
      $('#my1, #my2').get()
    );

    var $merged= "\nMerged:\n";
    
    $($p).each(
      function() {
        $merged += this.id + "\n";   
      }
    );

    $p = $.unique($p);
    
    var $after = "\nAfter\n";
    
    $($p).each(
      function() {
        $after += this.id + "\n";
      }
    );
  
    alert(
      $before + $merged + $after
    );  
  }
);
    </script>

  </head>
  <body>
    <p id='my1'>
    </p>
    <p id='my2'>
    </p>
    <p id='my3'>
    </p>
  </body>
</html>








30.67.merge
30.67.1.Merge array
30.67.2.Merge two arrays
30.67.3.Merge array and get the unique