반응형
concat() 메서드
concat() 메서드는 인자로 주어진 배열이나 값들을 기존 배열에 합쳐서 새로운 배열을 반환합니다.
- 기존배열이 변경되지 않습니다.
- 추가된 새로운 배열을 반환합니다.
const array1 = ['안녕', '하세요', '저는'];
const array2 = ['junhyeok', '입니다.', '잘부탁드려요!'];
const array3 = array1.concat(array2);
console.log(array3); // ['안녕', '하세요', '저는', 'junhyeok', '입니다.', '잘부탁드려요!']
array.concat([value1[, value2], ...[, valueN]]);
매개변수
- 배열 또는 값
- 만약 value1 ~ valueN 인자를 생략하면 기존배열의 얕은 복사를 반환.
반환값
새로운 Array 객체.
여러가지 예제
배열 두 개 이어붙이기
const a = ['a', 'b', 'c'];
const b = [1, 2, 3];
console.log(a.concat(b)); // ['a', 'b', 'c', 1, 2, 3]
배열 세 개 이어붙이기
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [7, 8, 9];
console.log(a.concat(b, c)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
배열에 값 이어붙이기
const a = ['a', 'b', 'c'];
console.log(a.concat(1, [2, 3])); // ['a', 'b', 'c', 1, 2, 3]
반응형
'JavaScript | TypeScript > javascript 문법' 카테고리의 다른 글
[ Javascript ] Array.prototype.fill() (0) | 2023.01.02 |
---|---|
[ Javascript ] Array.prototype.every() (0) | 2022.12.28 |
[ Javascript ] Array.prototype.entries() (0) | 2022.12.26 |
[ Javascript ] Array.prototype.copyWithin() (2) | 2022.12.22 |
[ Javascript ] Array.prototype.at() (0) | 2022.12.20 |