File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
javascript/4_array-programs Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ // 40. Merge Two Arrays Without Creating a New Array
2+
3+ {
4+ let array1 = [ 1 , 2 , 3 ] ;
5+ let array2 = [ 4 , 5 , 6 ] ;
6+
7+ array1 . push ( ...array2 ) ;
8+
9+ console . log ( array1 ) ;
10+ }
11+
12+ // Using Array concat() Method
13+
14+ {
15+ let array1 = [ 1 , 2 , 3 ] ;
16+ let array2 = [ 4 , 5 , 6 ] ;
17+
18+ // Store the merged array to first array
19+ array1 = array1 . concat ( array2 ) ;
20+
21+ console . log ( array1 ) ;
22+ }
23+
24+ // Using Array splice() Method
25+
26+ {
27+ let array1 = [ 1 , 2 , 3 ] ;
28+ let array2 = [ 4 , 5 , 6 ] ;
29+
30+ array1 . splice ( array1 . length , 0 , ...array2 ) ;
31+
32+ console . log ( array1 ) ;
33+ }
34+
35+ // Using a for Loop
36+
37+ {
38+ let array1 = [ 1 , 2 , 3 ] ;
39+ let array2 = [ 4 , 5 , 6 ] ;
40+
41+ for ( let i = 0 ; i < array2 . length ; i ++ ) {
42+ array1 . push ( array2 [ i ] ) ;
43+ }
44+
45+ console . log ( array1 ) ;
46+ }
You can’t perform that action at this time.
0 commit comments