. Advertisement .
..3..
. Advertisement .
..4..
JavaScript is a vast language with multiple types of projects to make any website interactive enough to interact with users. Learning to add array to an array in JavaScript is also a part of a project that programmers work on, even beginners try it to get through the code efficiently.
Let’s dig it to know how to add array to array in JavaScript
How To Add Array to Array in JavaScript
An array is a linear data structure and the most recognized structure used in the computer science world. To modify an array, the position of the array matters when adding it to another. You can add an element at three places; beginning, ending, and anywhere else.
Different methods are used to add array at different places
Using unshift() Method
This method is used to add one or more array objects to the beginning/start of an array. In return, you will get the new array length.
Code example 1
const startArr = [19, 29, 28, 22];
const newArrLength = startArr.unshift(12);
console.log(newArrLength); // Length of the new array
console.log(startArr); // Show array after adding element "12" at the beginning
startArr.unshift(-88, 89, 15, -18);
console.log(startArr); // Show array after adding a new array at the beginning
Output
5
[ 12, 19, 29, 28, 22 ]
[ -88, 89, 15, -18, 12, 19, 29, 28, 22]
Code example 2
var cars = ["Mercedes", "Audi", "Porsche", "Lexus"];
// Adding a single element to the cars array
cars.unshift("Ferrari");
console.log(cars); // Prints: ["Ferrari", "Mercedes", "Audi", "Porsche", "Lexus"]
// Adding multiple elements to the cars array
cars.unshift("BMW", "Bentley");
console.log(cars); // Prints: ["BMW", "Bentley", "Ferrari", "Mercedes", "Audi", "Porsche", "Lexus"]
// Loop through the cars array and display all the values
for(var i = 0; i < cars.length; i++){
document.write("<p>" + cars[i] + "</p>");
}
Output
BMW
Bentley
Ferrari
Mercedes
Audi
Porsche
Lexus
Using push() Method
To add one or more array objects at the end of an array, the push()
method is used. It also returns the array’s new length.
Code Example 1
const pushArr = [25, 8, 10, 12]
const newArrLength = pushArr.push(11, 17, 19, 7);
console.log(newLength);
console.log(pushArr);
Output
8
[ 25, 8, 10, 12, 11, 17, 19, 7 ]
Code Example 2
Let’s suppose you have two arrays; phones and brands
let phones = ['iphone', 'galaxy', 'passport', 'N95'];
let brands = ['apple', 'samsung', 'nokia', 'blackberry'];
for (const brand of brand) {
phones.push(brand);
}
console.log(phones);
Output
['iphone', 'galaxy', 'passport', 'N95', 'apple', 'samsung', 'nokia', 'blackberry']
Using concat() Method
This method is used to join or merge two or more arrays. With this method, you can create a new output copy without affecting the original arrays. In return, you will get a new array.
const example1Arr1 = [15, 21, 18];
const valuesAdd = [1, 6, 9, 88];
const example1NewArr = example1Arr1.concat(valuesAdd);
console.log(example1NewArr);
console.log(example1Arr1);
Output
[15, 21, 18, 1, 6, 9, 88]
[15, 21, 18]
To concatenate an array with multiple arrays
const arr1 = [2, 4, 6, 8];
const arr2 = [10, 12, 14, 16];
const arr3 = [18, 20, 22, 24];
const arr123= arr1.concat(arr2, arr3);
console.log(arr123);
Output
[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
Using spread(…) Operator
To merge an array, this method is used. A new array is created along with merging all arrays.
const countries1 = ['Denmark', 'Wales', 'Germany'];
const countries2 = ['Finland', 'Sweden', 'Argentina'];
const all = [...countries1, ...countries2];
all;
Output
['Denmark', 'Wales', 'Germany', 'Finland', 'Sweden', 'Argentina']
Using splice() Method
With this method, you can add an element anywhere, in-between, start, end, or in the middle. It is an array management method as it adds, replaces, and even removes an element.
const months = ['January', 'February', 'April', 'May']
const deletedArr = weekdays.splice(2, 0, 'March');
console.log(months);
console.log(deletedArr);
//In this example, we added ‘March’ in between
Output
['January', 'February', 'March', 'April', 'May']
[]
Conclusion
With all these methods to add an array to an array in JavaScript, I hope you will run the project effectively.
Feel free to share your opinion!
Leave a comment