Javascript

deep dive(...)

min' 2022. 12. 10. 02:24
728x90
반응형
const obj = {
  name : '유승민',
  title : '개발자',
  mentor: {
    name : '존',
    title: '시니어개발자',
  }
}


const name = '업데이트한 이름';
const update = {
  ...obj, // spread 연산자를 이용하여 object 객체를 낱개로 펼쳐서 복사해줌
  mentor: { ...obj.mentor, name },
  // 복사해온 것 중 mentor를 업데이트 하고 싶어서 덮어씌워주려고 함
  // mentor 안에는 또 객체가 있고, 거기서 다시 obj.mentor를 펼쳐서 복사해준 다음 name만 덮어씌워줌
};
728x90
반응형