Merge array in jQuery

Merge two arrays in jquery

The merge() function used to merge two arrays into single array in jquery.
This function Merges the values of the second array into the first and returns the result. The first array is modified by this operation and returned as the result. The code will we: $.merge(array1, array2)

Note: If you use array1 in future then you should keep a copy of array1, because merge() function modify original array1 and no change in array2.

<!DOCTYPE html>
  <title>jQuery With Example</title>
  <script src="" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('#btnMerge').click(function () {
        var Arr1 = [1, 2, 3];
        var Arr2 = [4, 5, 6];
        $.merge(Arr1, Arr2);
        var val = "";
        for (var i = 0; i < Arr1.length; i++) {
          val += Arr1[i] + ",";
        val = val.slice(0, -1) //remove last comma
    <input type="button" id="btnMerge" value="Merge" class="Button" />

No comments :

Please Give Your Feedback