How to use the push() method In Javascript
We can use the ‘push’ method to insert one array into another and merge the two arrays. This approach will modify the first array in place and won’t create a new array.
Example: In the below example we are using .push to merge to arrays.
const fruit1 = ['Mango', 'Apple', 'Orange'];
const fruit2 = ['Banana', 'Avacado', 'Watermelon'];
fruit2.forEach((element) => {
fruit1.push(element);
});
console.log(fruit1);
Output
[ 'Mango', 'Apple', 'Orange', 'Banana', 'Avacado', 'Watermelon' ]
JavaScript Program to Merge Two Arrays Without Creating a New Array
In this article, we will learn how to merge two arrays without creating a new array. In JavaScript Array is used to store elements of similar data types. If we want to add an element into another array for merging two arrays, we can use the push method. There are many methods for merging two arrays without creating a new array.
Table of Content
- Method 1: Using the push() method
- Method 2: Using splice() Method
- Method 3: By using push.apply method
- Method 4: Using concat with Assignment
Contact Us