jQuery merge - 배열 합치기

반응형
$.merge(first, second)

첫번째 배열에 두번째 배열을 추가하여 합치게 된다.

 

배열을 보존하면서 합치고 싶을때는?

merge 하기 전에 함수를 만들어 준다.

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

 

Sample

var hero = ['superman', 'batman', 'pororo'];
var person = ['짱구', '짱아', '신형만'];

var array = $.merge($.merge([], hero), person));

console.log(array);
console.log(hero);

결과

["superman","batman","pororo","짱구","짱아","신형만"]

["superman","batman","pororo"]

 

jQuery.merge()

 

jQuery.merge() | jQuery API Documentation

Description: Merge the contents of two arrays together into the first array. 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.

api.jquery.com

 

728x90
반응형