jQuery.merge()

Merge the contents of two arrays together into the first array.

jQuery.merge(first, second)🡢 Array

first ArrayLikeObjectThe first array-like object to merge, the elements of second added.
second ArrayLikeObjectThe second array-like object to merge into the first, unaltered.

The $.merge() operation forms an array that contains all elements from the two arrays. The orders of items in the arrays are preserved, with items from the second array appended. The $.merge() function is destructive. It alters the length and numeric index properties of the first object to include items from the second.

If you need the original first array, make a copy of it before calling $.merge(). Fortunately, $.merge() itself can be used for this duplication:

var newArray = $.merge([], oldArray);

This shortcut creates a new, empty array and merges the contents of oldArray into it, effectively cloning the array.

Prior to jQuery 1.4, the arguments should be true Javascript Array objects; use $.makeArray if they are not.

Merges two arrays, altering the first argument.

HTML
$.merge([0, 1, 2], [2, 3, 4]);
DEMO

Merges two arrays, altering the first argument.

HTML
$.merge([3, 2, 1], [4, 3, 2]);
DEMO

Merges two arrays, but uses a copy, so the original isn't altered.

HTML
var first = ["a", "b", "c"];
var second = ["d", "e", "f"];
$.merge($.merge([], first), second);
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)