Object.assign

LEE JI YOUNG·2022년 2월 23일
0

JS/Node

목록 보기
12/23
const initialState =
{
  "cartItems": [
    {
      "itemId": 1,
      "quantity": 1
    },
    {
      "itemId": 5,
      "quantity": 7
    },
    {
      "itemId": 2,
      "quantity": 3
    }
  ]
};

const a = Object.assign({}, initialState, {
        cartItems: initialState.cartItems.slice(1)
      })
console.log(a)

// 가장 마지막 값으로 덮어지기때문에 잘린값이 최종으로 나타나게 된다.
// {
//   cartItems: [
//     { itemId: 5, quantity: 7 },
//     { itemId: 2, quantity: 3 }
//   ]
// }

console.log(initialState)

// {
//   cartItems: [
//     { itemId: 1, quantity: 1 },
//     { itemId: 5, quantity: 7 },
//     { itemId: 2, quantity: 3 }
//   ]
// }

let state = {notification : [1, 2, 3]}

console.log(Object.assign({}, state, { notification : state.notification.slice(1)}))

// { notification: [ 2, 3 ] }


console.log(Object.assign({}, {notification : [1, 2, 3]}, { notification : state.notification.slice(1)}))
// { notification: [ 2, 3 ] }
profile
프론트엔드 개발자

0개의 댓글