Skip to content

Commit b074360

Browse files
committed
mergeTwoArraysWithoutNewArrays.js
1 parent da829e8 commit b074360

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)