Home JavaScript 팁 모음 ! (02)
Post
Cancel

JavaScript 팁 모음 ! (02)

JavaScript 팁을 모아봤습니다.

JavaScript 팁 모음 ! (02)

조건부로 속성 추가하기

1
2
3
4
5
6
7
8
9
const isValid = false;
const age = 18;
const person = {
  id: "ab32",
  name: "Krina",
  ...(isValid && { isActive: true }),
  ...((age >= 18 || isValid) && { cart: 0 })
};
console.log(person); // { id: "ab32", name: "Krina", cart: 0 }

동적 키를 사용해 객체 분해

1
2
3
4
const productData = { id: "23", name: "Laptop" };
const extractKey = "name";
const { [extractKey]: data } = productData;
console.log(data); // Laptop

배열에서 falsy 값 제거

  • Boolean 사용하기
1
2
3
4
5
const fruitList = ["apple", null, "mango", undefined, ""];
const filterFruitList = fruitList.filter(Boolean);
console.log(filterFruitList); // [ "apple", "mango" ]
const isAnyFruit = fruitList.some(Boolean);
console.log(isAnyFruit); // true

배열에서 중복 값 제거

  • Set 사용하기
1
2
3
const fruitList = ["apple", "mango", "apple", "grapes"];
const uniqFruitList = [...new Set(fruitList)];
console.log(uniqFruitList); // [ "apple", "mango", "grapes" ]

숫자를 문자열로 쉽게 변환

  • 문자열은 앞에 +를 붙여주기
  • 숫자에 빈 문자열을 더해 문자열로 만들기
1
2
const personId = 324743;
console.log(personId + "", typeof (personId + "")); // personId string

참고

11 Useful Modern JavaScript Tips

This post is licensed under CC BY 4.0 by the author.