JavaScript Tutorial.45

ansunny1170·2021년 12월 31일
0
post-thumbnail

JS Sets

JavaScript Set는 고유한 값의 모음이다.
각 값은 Set에서 한 번만 발생할 수 있다.

Essential Set Methods

MethodDescription
new Set()Creates a new Set
add()Adds a new element to the Set
delete()Removes an element from a Set
has()Returns true if a value exists in the Set
forEach()Invokes a callback for each element in the Set
values()Returns an iterator with all the values in a Set
PropertyDescription
sizeReturns the number of elements in a Set

How to Create a Set

아래와 같이JavaScript Set 생성할 수 있다.

  • new Set()에 배열 전달
  • new Set을 만들고 add()를 사용하여 값을 추가
  • new Set을 만들고 add()를 사용하여 변수를 추가

The new Set() Method

new Set() 생성자에 배열을 전달한다:

Set을 생성하고 값을 add한다.

Set을 생성하고 변수를 add한다.

The add() Method


만약 동일한 요소를 add한다면, 첫 번째만 저장될 것이다.

The forEach() Method

forEach() 메서드는 각 Set 요소에 대한 함수를 호출한다.

The values() Method

values() 메서드는 Set의 모든 값을 포함하는 새 반복자 객체를 반환한다.
values()메서드는 객체에 대한 모든 을 의미한다.

이제 Iterator 객체를 사용하여 요소에 액세스할 수 있다.

profile
공정 설비 개발/연구원에서 웹 서비스 개발자로 경력 이전하였습니다. Node.js 백엔드 기반 풀스택 개발자를 목표로 하고 있습니다.

0개의 댓글