JavaScript DHTML/jQuery/merge
Merge array
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ alert($.merge( [0,1,2], [2,3,4] ));
}); </script> </head> <body> <body>
</body>
</html>
</source>
Merge array and get the unique
<source lang="html4strict">
<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>
</body>
</html>
</source>
Merge two arrays
<source lang="html4strict">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript">
$(document).ready(
function() { var $vegetables = [ "C", "T", "L" ]; var $fruits = [ "C", "A", "C" ]; $merged = $.merge($vegetables, $fruits); var $string = ""; $($merged).each( function() { $string += this + "\n"; } ); alert($string); }
);
</script> </head> <body> </body>
</html>
</source>