Let's use a Map!

로선생·2024년 6월 11일
0

a Map object consists of sets of keys and values.

A Map is similar to an Object, but they are differences.

A Map object can be created by a generator function.

if there are arguments in a generator function,
the arguments should be an iterable containing sets of keys and values.
And the key must not be duplicated.

const map = new Map()
const map_arguments = new Map([['key', 'value'], ['key1', 'value1']])

properties for a Map

  • size

it checks the number of elements in a Map.
it has only a getter function without ㅁ setter function,
so it can not be replaced with a value.

  • set

it is used for adding an element in a Map.

** Usually, a key type of Object should be a String or Symbol.
But, a Map does not have limitations on key value types.

let max = new Map();

// map
max.set('id', 0);
max.set('이름', '마이클');
max.set('전공', '영문학');
max.set('나이', 25);

// get
max.get('이름'); // "마이클"

// delete
max.delete('나이'); // true

// clear
max.clear();
profile
이제는 이것저것 먹어요

0개의 댓글