1.

Merge two arrays in JavaScript

Answer»

Let’s say the following are our arrays: 

[20, 40, 60] [80, 100, 120]

To merge them, JavaScript provides the ARRAY.concat() METHOD. The return value of this method is a new array with the RESULT of concatenated arrays: 

var res = arr1.concat(arr2);

Let us now merge the above two arrays: 

<html>    <head>       <title>Merge JavaScript Arrays</title>    </head>    <body>       <SCRIPT>          var arr1 = [20, 40, 60];          var arr2 = [80, 100, 120];          document.write("Array 1 = " + arr1);          document.write("<br>Array 2 = " + arr2 );          var res = arr1.concat(arr2);          document.write("<br>Merged Arrays = " + res );       </script>    </body> </html>

The output displays the merged arrays: 

Array 1 = 20,40,60 Array 2 = 80,100,120 Merged Arrays = 20,40,60,80,100,120


Discussion

No Comment Found