JavaScript Tutorial.46

ansunny1170·2022년 1월 2일
0
post-thumbnail

JS MAPS

Map은 키가 모든 데이터 유형이 될 수 있는 키-값 쌍을 보유한다.
Map은 키의 최초의 삽입 순서를 기억한다.

Essensial Map Methods

MethodDescription
new Map()Creates a new Map
set()Sets the value for a key in a Map
get()Gets the value for a key in a Map
delete()Removes a Map element specified by the key
has()Returns true if a key exists in a Map
forEach()Calls a function for each key/value pair in a Map
entries()Returns an iterator with the [key, value] pairs in a Map
PropertyDescription
sizeReturns the number of elements in a Map

How to Create a Map

아래 와 같이 JavaScript Map을 생성 할 수 있다 :

  • Passing an Array to new Map()
  • Create a Map and use Map.set()

The new Map() Method

new Map() 생성자에 Array를 전달하여 Map을 생성할 수 있다.

The set() Method

set() 메소드를 사용하여 맵에 요소를 추가할 수 있다.

또한,set()메서드는 이미 존재하는 Map의 값을 변경하는데에도 사용 할 수 있다.

The get() Method

get()메서드는 Map안의 키에 대한 값을 가질(호출할) 수 있다.

The size Property

size 속성은 Map의 요소의 갯수를 반환한다.

The delete() Method

delete() 메서드는 Map의 요소를 삭제한다.

The has() Method

has() 메서드는 Map에 해당하는 키가 있으면 true를 반환한다.

JavaScript Objects vs Maps

JavaScript Objects와 Maps의 차이점:

ObjectMap
IterableNot directly iterableDirectly iterable
SizeDo not have a size propertyHave a size property
Key TypesKeys must be Strings (or Symbols)Keys can be any datatype
Key OrderKeys are not well orderedKeys are ordered by insertion
DefaultsHave default keysDo not have default keys

The forEach() Method

forEach() 메서드는 Map의 각 키/값 쌍에 대한 함수를 호출한다.

The entries() Method

entries() 메서드는 Map에 [key, values]이 있는 반복자 객체를 반환한다.

Browser Support

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

0개의 댓글