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






Merge array and get the unique

 



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

   
  








Related examples in the same category

1.Merge array
2.Merge two arrays