$.merge

jQuery.merge( first, second ) 返回: Array

描述: 合并两个数组内容到第一个数组。

  • version added: 1.0 jQuery.merge( first, second )

    first 第一个用来合并的数组,元素是第二数组加进来的。

    second 第二个数组合并到第一,保持不变。

$.merge()操作形成一个数组,其中包含两个数组的所有元素。将第二个数组直接追加在第一个数组后面。$.merge()函数是破坏性的。它改变了第一个数组。

如果不想改变第一个数组,可以设置第一个参数为空数组,将你要复制的数组作为第二个参数:

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

此快捷方式创建一个新的,空数组合并了oldArray的内容,有效地克隆了数组。

Examples:

Example: Merges two arrays, altering the first argument.

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

Result:

[0,1,2,2,3,4] 

Example: Merges two arrays, altering the first argument.

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

Result:

[3,2,1,4,3,2] 

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

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

Result:

["a","b","c","d","e","f"]