How do you merge two arrays in JavaScript/jQuery?
Method 1: Traditional or simple way – using loop
Iterating on one of two arrays, get each element and append it into the third array.
const arr1 = ['A','B','C','D'];
const arr2 = ['a','b','c','d'];
let arr3 = [];
for(let i = 0,len = arr1.length; i < len; i++){
arr3[arr3.length] = arr1[i];
arr3[arr3.length] = arr2[i];
}
console.log(arr3);
Method 2: Using $.merge function
jQuery provides $.merge a function to do this request.
const arr1 = ['A','B','C','D']; const arr2 = ['a','b','c','d']; const arr3 = $.merge(arr1, arr2)
That’s it!
