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.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" 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
        alert(val);
      });
    });
  </script>
</head>
<body>
  <div>
    <input type="button" id="btnMerge" value="Merge" class="Button" />
  </div>
</body>
</html>
DISPLAY

No comments :

Please Give Your Feedback