세트

王林
풀어 주다: 2024-08-26 21:42:32
원래의
281명이 탐색했습니다.

세트

  • ES6 이전에는 JS에 내장된 데이터 구조가 배열과 객체라는 두 가지뿐이었습니다. ES6에서는 두 가지 새로운 DS: 세트, 맵을 도입했습니다.
  • 어레이를 보관하고 질서 있게 회수하기 위해 사용합니다.
  • 세트는 순서가 중요하지 않은 경우에만 사용하고 데이터 구조 내부에 요소가 있는지 확인하면 됩니다.
  • 세트는 어레이를 대체하기 위한 것이 아닙니다. 배열이 더 중요합니다.

- 중복된 값과 함께 값을 순서대로 저장해야 할 때마다 배열을 사용해야 합니다.

## Usecase: To remove duplicate values of arrays.
const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger'];

const items = new Set(order);
items; // All duplicate values are removed

const city = new Set("California").size;
city; // 8
로그인 후 복사

세트:

  • Set도 String처럼 반복 가능합니다.
  • 고유한 가치의 집합.
const city = new Set("California");
city;     // Set(8) { 'C', 'a', 'l', 'i', 'f', 'o', 'r', 'n' }

로그인 후 복사
## Difference between Set & Array:
1. Although looks similar to array, but it has no key-value pairs. Hence, set[0] is invalid.
2. Only a list of unique values, all duplicate removed.
3. Order of element is irrelevant

## Similarities between Arrays & 세트:
1. Set has size property, Array has length propery.
2. Set has 'has' method, Array has includes method.

const order = ['pizza','burger','pasta','noodles','pizza','noodles','burger'];

const items = new Set(order);
items; // Set(4) { 'pizza', 'burger', 'pasta', 'noodles' }


//Both array and sets are iterables. Hence easier to convert from sets to array.
[...items];
로그인 후 복사

고급: 혼합된 데이터 유형을 보유할 수 있지만 중복될 수는 없습니다.
가장 일반적인 반복 가능 항목은 Array입니다. 전. 구문: new Set(반복 가능)

세트의 메소드 및 속성 목록:

.size;  // returns a numerical value
.has('name'); // returns a boolean value
.add('name');  // returns the set with added value
.delete('name'); // returns a boolean value
.clear(); // deletes all elements. returns Set(0) {} 


로그인 후 복사

위 내용은 세트의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!