LoginSignup
0
0

More than 1 year has passed since last update.

서로다른코드입니까?

Posted at

#코드비교
filter안에 화살표함수를 사용했는데 결과값이 다르다.
아래 두코드가 다른코드입니까?

code1
   addItem(state, item) {
// 여기부터
      const cartItems = state.items.filter(
        (cartItem) => cartItem.id === item.id
      );
// 여기까지
      if (cartItems.length === 0) {
        state.items.push({
          ...item,
          qty: 1,
        });
      } else {
        cartItems[0].qty++;
      }
    },
code2
    addItem(state, item) {
// 여기부터
      const resultItems = state.items.filter((cartItem) => {
        cartItem.id === item.id;
      });
// 여기까지
      if (resultItems.length === 0) {
        state.items.push({
          ...item,
          qty: 1,
        });
      } else {
        resultItems[0].qty++;
      }
    },
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0