세트
-
[모던 자바스크립트] 세트, 문자열 패딩프론트엔드 개발자가 될거야./js 2023. 4. 19. 15:37
set (세트) 세트란 어떠한 자료형의 값이든 각 원소를 고유하게 저장하는 객체이다. 고유한 값만 보유할 수 있는 set의 특징을 이용하여 배열에서 중복을 제거할 수 있다. const myArray = ['a', 'b', 'c', 'd', 'a', 'a', 'b', 'b']; const set = new Set(myArray); console.log(set); // Set {'a', 'b', 'c', 'd'}; // Set을 배열로 변환하기 const uniqueArray = Array.from(set); console.log(uniqueArray); // ['a', 'b', 'c', 'd']; // 한줄로도 구현가능 const uniqueArray = Array.from(new Set(myArray))..