Set 문법

1. 배열 형태를 가지는 객체 데이터

a = new Set([1,2,3,2])
b = [1,2,3]

typeof a 	// 'object'
Array.isArray(a) // false
Array.isArray(b) // true

2. 고유한 값만 저장 (중복 데이터가 들어오지 않음)

// 데이터 추가
a.add(4)		// Set(4) {1, 2, 3, 4}

// 데이터 삭제
a.delete(2) 	// Set(3) {1, 3, 4}

// 데이터 조회
a.has(4)		// true

// 길이 조회
a.size			// 3

// 데이터 리셋 - 비워짐
a.clear()

// 반복문 사용
a.forEach(el => {
   console.log(el)    // 1, 3, 4
})

// 배열로 변환
c = Array.from(a)		//[1, 3, 4]
profile
프론트엔드 개발자 도전기

0개의 댓글

Powered by GraphCDN, the GraphQL CDN