react

[6] React ES6 Spread Operator

suye0n6 2025. 3. 28. 12:01

Spread Operator

JavaScript의 스프레드 연산자( . . . )를 사용하면 기존 배열이나 객체의 전체 또는 일부를 다른 배열이나 객체로 빠르게 복사할 수 있다.

 

const numbersOne = [1, 2, 3];
const numbersTwo = [4, 5, 6];
const numbersCombined = [...numbersOne, ...numbersTwo];

 

스프레드 연산자는 종종 구조 분해와 함께 사용된다.

 

첫 번째와 두 번째 항목을 numbers 변수에 할당하고 나머지는 배열에 넣습니다.

const numbers = [1, 2, 3, 4, 5, 6];

const [one, two, ...rest] = numbers;

 

객체에도 스프레드 연산자를 사용할 수 있다.

const myVehicle = {
  brand: 'Ford',
  model: 'Mustang',
  color: 'red'
}

const updateMyVehicle = {
  type: 'car',
  year: 2021, 
  color: 'yellow'
}

const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}