Merge two or more arrays

Write a function that takes two or more arrays and returns a modified array containing the elements of the input arrays in the order they were passed to the method. The original array is modified.


Example ...

Enter two arrays to merge ...

[1, 2, 3, 4] array 1 *

[5, 6, 7, 8] array 2

[1, 2, 3, 4, 5, 6, 7, 8] merged arrays

* array 1 is modified.

Enter three arrays to merge ...

[1, 2, 3, 4] array 1 *

[5, 6, 7, 8] array 2

[9, 10, 11, 12] array 3

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] merged arrays

* array 1 is modified.

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Merge two or more arrays using ↴

spread syntax ... allows an iterable, such as an array, to be expanded in places where zero or more arguments or elements are expected.

push() method → adds specified elements to the end of an array and returns the new length of the array.


spread syntax ... allows an iterable, such as an array, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.

spread syntax spreads the array elements into individual arguments.

Example

Math.max() built-in method returns the largest of the zero or more numbers given as input.

It can take multiple arguments but does not accept an array directly.

const arr2 = [2, 6, 10, 4, 8];

Math.max(arr2); returns ↴

NaN Not a Number

When ...arr is used in the function call, it expands the iterable object arr into a list of arguments.

const arr3 = [2, 6, 10, 4, 8];

Math.max(...arr3); returns ↴

10 → largest number

console.log(arr3); returns ↴

[2, 6, 10, 4, 8] → array

console.log(...arr3); returns ↴

2 6 10 4 8 → list of arguments


push() method adds new elements to the end of an array.

Add 4 to end of array.

const arr4 = [1, 2, 3];

arr4.push(4);

console.log(arr4); returns ↴

[1, 2, 3, 4]4 added to end of array

The push() method changes the length of the array.

arr4 is modified.

Using the spread operator creates a new array.

Add 4 to a new array.

const arr5 = [1, 2 , 3];

const arr6 = [...arr5, 4];

console.log(arr6); returns ↴

[1, 2, 3, 4]4 added to new array

console.log(arr5); returns ↴

[1, 2 ,3]

arr5 remains unchanged.


Initialize variables to hold the two arrays to merge.

const array1 = [1, 2, 3, 4 ]; → user input

const array2 = [5, 6, 7, 8]; → user input


Define a function mergeArrays to merge two or more arrays.

function mergeArrays(arr1, arr2)

The function takes two arrays as input arr1, arr2 and returns arr2 merged into arr1

The returned array arr1 is modified.

arr1 serves as the primary array that will be modified to include the elements of arr2

push() method only accepts a list of arguments, not an array. The spread operator passes an array as a list of arguments.

arr1.push(...arr2) spread operator expands arr2 and pushes its elements into arr1

Return the merged array.

return arr1arr1 is modified.

If merging more than two arrays.

function mergeArrays(arr1, arr2, arr3)

The functions returns arr2 and arr3 merged into arr1

The returned array arr1 is modified.


Call the function with ↴

mergeArrays(array1, array2);


Merge two arrays.

const array1 = [1, 2, 3, 4];

const array2 = [5, 6, 7, 8];

function mergeArrays(arr1, arr2) {

arr1.push(...arr2);

return arr1;

}

call function

mergeArrays(array1, array2); returns ↴

[1, 2, 3, 4, 5, 6, 7, 8]


Merge three arrays.

const array1 = [1, 2, 3, 4];

const array2 = [5, 6, 7, 8];

const array3 = [9, 10, 11, 12];

function mergeArrays(arr1, arr2, arr3) {

arr1.push(...arr2, ...arr3);

return arr1;

}

call function

mergeArrays(array1, array2, array3); returns ↴

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]


Alternative using for loop and push() method to merge two arrays only ↴

const array1 = [1, 2, 3];

const array2 = [4, 5, 6];

function mergeArrays2(arr1, arr2) {

for (let i = 0; i < arr2.length; i++) {

arr1.push(arr2[i]);

}

return arr1;

}

call function

mergeArrays2(array1, array2); returns ↴

[1, 2, 3, 4, 5, 6]

Merge two or more arrays