YoTo Blog

JavaScript Map


JavaScript Map

A JavaScript map is an object composed of key-value pairs. Unlike a normal Object, it also remembers the order.

  • Declare
const map01 = new Map();
  • Insert a value
map01.set('key01', 123);
map01.set('key03', 999);
map01.set('key02', 456);
  • Load a value
map01.get('key01'); // 123
 
map01.get('key03');
// 999
  • Load values ​​in order
map01.forEach((value, key, map) => {
console.log(`${key} : ${value}`);
})
// key01 : 123
// key03 : 999
// key02 : 456
  • Delete values
map01.delete('key01');
  • Initialize all values
map01.clear();

Other

Only frequently used functions are introduced.

In addition, there are keys, has, values, etc.

General key-value format Objects are also used, but if you use Map, you can maintain the order of the data, so you should use it depending on the situation.