jQuery.merge()
S2C Home « Utilities « jQuery.merge()
Merge two arrays.
Description
The jQuery.merge()
jQuery General utility method, merges two arrays into the first specified array.
Shorthand version $.merge()
- Prior to version 1.4 of jQuery, the
jQuery.merge()
jQuery General utility method will only work onArray
objects. - In versions of jQuery prior to 1.4, array-like objects (objects containing a length property with a value), can be converted to arrays using the
jQuery.makeArray()
jQuery General utility method, before using this method. - The
jQuery.merge()
jQuery General utility method is destructive in the fact that it alters the first parameter, by appending the items from the second parameter. Using an empty array as the first argument will effectively clone the second array to the empty array.
Syntax
Signature | Description |
---|---|
jQuery.merge( first, second ) | Merge two arrays into the first specified array. |
Parameters
Parameter | Description | Type |
---|---|---|
first | The first array to append the elements of the second array to. | Array |
second | The second array to merge into the first array. | Array |
Return
An Array
object.
jQuery.merge( first, second )
ExampleTop
Merge two arrays into the first specified array.
In the example below when we press the button the first time we create an an object and then flatten it to an array using the jQuery.merge()
jQuery General utility method. An interesting thing to note is
how we lose the original skills
key in the flattening. You can use the jQuery.extend() method for these situations.
$(function(){
$('#btn20').one('click', function(){
var arr0 = [7, 4, 3, 1, 5];
var arr1 = [5, 1, 3, 4, 7];
$.merge( arr0, arr1 );
$('#div16').append('First array values: ' + JSON.stringify(arr0) + '<br>');
$('#div16').append('Second array is unchanged: ' + JSON.stringify(arr1));
});
});
div16. Some initial text.
Related Tutorials
jQuery Intermediate Tutorials - Lesson 9 - jQuery General Utilities