객체분해 예제

2023. 1. 4. 13:37코드스테이츠/예제모음

 const user = {name: '김코딩',company: {name: 'Code States',department: 'Development',role: {name: 'Software Engineer'}},age: 35}

    const changedUser = { // {name: '박해커',company: {name: 'Code States',department: 'Development',role: {name: 'Software Engineer'}},age: 20}
      ...user,
      name: '박해커',
      age: 20
    }

    const overwriteChanges = { // {name: '박해커',age: 20}
      name: '박해커', 
      age: 20,
      ...user // 앞에 선언된 name과 age를 덮어써버린다.
    }

    const changedDepartment = { //{name: '김코딩',company: {name: 'Code States',department: 'Marketing',role: {name: 'Software Engineer'}},age: 35}
      ...user,
      company: {
        ...user.company,
        department: 'Marketing'
      }
    }

    expect(changedUser).to.eql({name: '박해커',company: {name: 'Code States',department: 'Development',role: {name: 'Software Engineer'}},age: 20})

    expect(overwriteChanges).to.eql({name: '김코딩',company: {name: 'Code States',department: 'Development',role: {name: 'Software Engineer'}},age: 35})

    expect(changedDepartment).to.eql({name: '김코딩',company: {name: 'Code States',department: 'Marketing',role: {name: 'Software Engineer'}},age: 35})
  })
})

'코드스테이츠 > 예제모음' 카테고리의 다른 글

Git 설정 및 기본 명령어  (0) 2023.04.12
spread / rest 예제  (0) 2023.01.03
스코프 예제  (0) 2023.01.03