๐Ÿ“˜Map

๐Ÿšง under construction

breadcrumb ๐Ÿšง

Map

้›–็„ถ Object ่ˆ‡ Map ้ƒฝๆ˜ฏ JS ็”จไพ†ๅฏฆ็พ Dictionary ็š„ๆ–นๆณ•๏ผŒไฝ†ๅฎƒๅ€‘ๅญ˜ๅœจๅŸบๆœฌ็š„ไธๅŒ้ปžใ€‚

Maps vs. Objects

feature
Map
Object
  • โœ… use [...map] to convert into Array.

  • โœ… for(const [key, value] of map){...}

  • โœ… for(const key of map.keys()){...}

  • โœ… for(const value of map.values()){...}

  • โœ… map.forEach((value, key) => {...})

keys

can be any value (including functions, objects, or any primitive).

must be either String or Symbol.

โœ… number of items in a Map

โŒ no such property

Map โ‡” Array

// Array => Map
const map = new Map([['key1', 'value1'], ['key2', 'value2']])

// Map => Array<[Key, Value]>
[...map]        // [['key1', 'value1'], ['key2', 'value2']]

// Map => Array<Value>
[...map.values()]

Other Topics

Last updated