{"id":199415,"date":"2020-09-04T10:08:44","date_gmt":"2020-09-04T02:08:44","guid":{"rendered":"https:\/\/gulass.cn\/?p=199415"},"modified":"2020-08-28T10:09:35","modified_gmt":"2020-08-28T02:09:35","slug":"map-and-set","status":"publish","type":"post","link":"https:\/\/gulass.cn\/map-and-set.html","title":{"rendered":"ES6 Map \u4e0e Set\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nMap \u5bf9\u8c61\u4fdd\u5b58\u952e\u503c\u5bf9\u3002\u4efb\u4f55\u503c(\u5bf9\u8c61\u6216\u8005\u539f\u59cb\u503c) \u90fd\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u952e\u6216\u4e00\u4e2a\u503c\u3002Set \u5bf9\u8c61\u5141\u8bb8\u4f60\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u552f\u4e00\u503c\uff0c\u65e0\u8bba\u662f\u539f\u59cb\u503c\u6216\u8005\u662f\u5bf9\u8c61\u5f15\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Map \u5bf9\u8c61<\/strong><\/div>\n

Map \u5bf9\u8c61\u4fdd\u5b58\u952e\u503c\u5bf9\u3002\u4efb\u4f55\u503c(\u5bf9\u8c61\u6216\u8005\u539f\u59cb\u503c) \u90fd\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u952e\u6216\u4e00\u4e2a\u503c\u3002<\/p>\n

Maps \u548c Objects \u7684\u533a\u522b<\/strong><\/div>\n
    \n
  1. \u4e00\u4e2a Object \u7684\u952e\u53ea\u80fd\u662f\u5b57\u7b26\u4e32\u6216\u8005 Symbols\uff0c\u4f46\u4e00\u4e2a Map \u7684\u952e\u53ef\u4ee5\u662f\u4efb\u610f\u503c\u3002<\/li>\n
  2. Map \u4e2d\u7684\u952e\u503c\u662f\u6709\u5e8f\u7684\uff08FIFO \u539f\u5219\uff09\uff0c\u800c\u6dfb\u52a0\u5230\u5bf9\u8c61\u4e2d\u7684\u952e\u5219\u4e0d\u662f\u3002<\/li>\n
  3. Map \u7684\u952e\u503c\u5bf9\u4e2a\u6570\u53ef\u4ee5\u4ece size \u5c5e\u6027\u83b7\u53d6\uff0c\u800c Object \u7684\u952e\u503c\u5bf9\u4e2a\u6570\u53ea\u80fd\u624b\u52a8\u8ba1\u7b97\u3002<\/li>\n
  4. Object \u90fd\u6709\u81ea\u5df1\u7684\u539f\u578b\uff0c\u539f\u578b\u94fe\u4e0a\u7684\u952e\u540d\u6709\u53ef\u80fd\u548c\u4f60\u81ea\u5df1\u5728\u5bf9\u8c61\u4e0a\u7684\u8bbe\u7f6e\u7684\u952e\u540d\u4ea7\u751f\u51b2\u7a81\u3002<\/li>\n<\/ol>\n
    Map \u4e2d\u7684 key<\/strong><\/span><\/div>\n

    key \u662f\u5b57\u7b26\u4e32<\/strong><\/p>\n

    var myMap = new Map();\r\nvar keyString = \"a string\"; \r\n \r\nmyMap.set(keyString, \"\u548c\u952e'a string'\u5173\u8054\u7684\u503c\");\r\n \r\nmyMap.get(keyString);    \/\/ \"\u548c\u952e'a string'\u5173\u8054\u7684\u503c\"\r\nmyMap.get(\"a string\");   \/\/ \"\u548c\u952e'a string'\u5173\u8054\u7684\u503c\"\r\n                         \/\/ \u56e0\u4e3a keyString === 'a string'<\/pre>\n

    key \u662f\u5bf9\u8c61<\/strong><\/p>\n

    var myMap = new Map();\r\nvar keyObj = {}, \r\n \r\nmyMap.set(keyObj, \"\u548c\u952e keyObj \u5173\u8054\u7684\u503c\");\r\n\ufeff\r\nmyMap.get(keyObj); \/\/ \"\u548c\u952e keyObj \u5173\u8054\u7684\u503c\"\r\nmyMap.get({}); \/\/ undefined, \u56e0\u4e3a keyObj !== {}<\/pre>\n

    key \u662f\u51fd\u6570<\/strong><\/p>\n

    var myMap = new Map();\r\nvar keyFunc = function () {}, \/\/ \u51fd\u6570\r\n \r\nmyMap.set(keyFunc, \"\u548c\u952e keyFunc \u5173\u8054\u7684\u503c\");\r\n \r\nmyMap.get(keyFunc); \/\/ \"\u548c\u952e keyFunc \u5173\u8054\u7684\u503c\"\r\nmyMap.get(function() {}) \/\/ undefined, \u56e0\u4e3a keyFunc !== function () {}<\/pre>\n

    key \u662f NaN<\/strong><\/p>\n

    var myMap = new Map();\r\nmyMap.set(NaN, \"not a number\");\r\n \r\nmyMap.get(NaN); \/\/ \"not a number\"\r\n \r\nvar otherNaN = Number(\"foo\");\r\nmyMap.get(otherNaN); \/\/ \"not a number\"<\/pre>\n

    \u867d\u7136 NaN \u548c\u4efb\u4f55\u503c\u751a\u81f3\u548c\u81ea\u5df1\u90fd\u4e0d\u76f8\u7b49(NaN !== NaN \u8fd4\u56detrue)\uff0cNaN\u4f5c\u4e3aMap\u7684\u952e\u6765\u8bf4\u662f\u6ca1\u6709\u533a\u522b\u7684\u3002<\/p>\n

    Map \u7684\u8fed\u4ee3<\/strong><\/span><\/div>\n

    \u5bf9 Map \u8fdb\u884c\u904d\u5386\uff0c\u4ee5\u4e0b\u4e24\u4e2a\u6700\u9ad8\u7ea7\u3002<\/p>\n

    for...of<\/strong><\/p>\n

    var myMap = new Map();\r\nmyMap.set(0, \"zero\");\r\nmyMap.set(1, \"one\");\r\n \r\n\/\/ \u5c06\u4f1a\u663e\u793a\u4e24\u4e2a log\u3002 \u4e00\u4e2a\u662f \"0 = zero\" \u53e6\u4e00\u4e2a\u662f \"1 = one\"\r\nfor (var [key, value] of myMap) {\r\n  console.log(key + \" = \" + value);\r\n}\r\nfor (var [key, value] of myMap.entries()) {\r\n  console.log(key + \" = \" + value);\r\n}\r\n\/* \u8fd9\u4e2a entries \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Iterator \u5bf9\u8c61\uff0c\u5b83\u6309\u63d2\u5165\u987a\u5e8f\u5305\u542b\u4e86 Map \u5bf9\u8c61\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684 [key, value] \u6570\u7ec4\u3002 *\/\r\n \r\n\/\/ \u5c06\u4f1a\u663e\u793a\u4e24\u4e2alog\u3002 \u4e00\u4e2a\u662f \"0\" \u53e6\u4e00\u4e2a\u662f \"1\"\r\nfor (var key of myMap.keys()) {\r\n  console.log(key);\r\n}\r\n\/* \u8fd9\u4e2a keys \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Iterator \u5bf9\u8c61\uff0c \u5b83\u6309\u63d2\u5165\u987a\u5e8f\u5305\u542b\u4e86 Map \u5bf9\u8c61\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u952e\u3002 *\/\r\n \r\n\/\/ \u5c06\u4f1a\u663e\u793a\u4e24\u4e2alog\u3002 \u4e00\u4e2a\u662f \"zero\" \u53e6\u4e00\u4e2a\u662f \"one\"\r\nfor (var value of myMap.values()) {\r\n  console.log(value);\r\n}\r\n\/* \u8fd9\u4e2a values \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Iterator \u5bf9\u8c61\uff0c\u5b83\u6309\u63d2\u5165\u987a\u5e8f\u5305\u542b\u4e86 Map \u5bf9\u8c61\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u3002 *\/<\/pre>\n

    forEach()<\/strong><\/p>\n

    var myMap = new Map();\r\nmyMap.set(0, \"zero\");\r\nmyMap.set(1, \"one\");\r\n \r\n\/\/ \u5c06\u4f1a\u663e\u793a\u4e24\u4e2a logs\u3002 \u4e00\u4e2a\u662f \"0 = zero\" \u53e6\u4e00\u4e2a\u662f \"1 = one\"\r\nmyMap.forEach(function(value, key) {\r\n  console.log(key + \" = \" + value);\r\n}, myMap)<\/pre>\n
    Map \u5bf9\u8c61\u7684\u64cd\u4f5c<\/strong><\/span><\/div>\n

    Map \u4e0e Array\u7684\u8f6c\u6362<\/strong><\/p>\n

    var kvArray = [[\"key1\", \"value1\"], [\"key2\", \"value2\"]];\r\n \r\n\/\/ Map \u6784\u9020\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a \u4e8c\u7ef4 \u952e\u503c\u5bf9\u6570\u7ec4\u8f6c\u6362\u6210\u4e00\u4e2a Map \u5bf9\u8c61\r\nvar myMap = new Map(kvArray);\r\n \r\n\/\/ \u4f7f\u7528 Array.from \u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a Map \u5bf9\u8c61\u8f6c\u6362\u6210\u4e00\u4e2a\u4e8c\u7ef4\u952e\u503c\u5bf9\u6570\u7ec4\r\nvar outArray = Array.from(myMap);<\/pre>\n

    Map \u7684\u514b\u9686<\/strong><\/p>\n

    var myMap1 = new Map([[\"key1\", \"value1\"], [\"key2\", \"value2\"]]);\r\nvar myMap2 = new Map(myMap1);\r\n \r\nconsole.log(original === clone); \r\n\/\/ \u6253\u5370 false\u3002 Map \u5bf9\u8c61\u6784\u9020\u51fd\u6570\u751f\u6210\u5b9e\u4f8b\uff0c\u8fed\u4ee3\u51fa\u65b0\u7684\u5bf9\u8c61\u3002<\/pre>\n

    Map \u7684\u5408\u5e76<\/strong><\/p>\n

    var first = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]);\r\nvar second = new Map([[1, 'uno'], [2, 'dos']]);\r\n \r\n\/\/ \u5408\u5e76\u4e24\u4e2a Map \u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\u503c\uff0c\u5219\u540e\u9762\u7684\u4f1a\u8986\u76d6\u524d\u9762\u7684\uff0c\u5bf9\u5e94\u503c\u5373 uno\uff0cdos\uff0c three\r\nvar merged = new Map([...first, ...second]);<\/pre>\n
    Set \u5bf9\u8c61<\/strong><\/div>\n

    Set \u5bf9\u8c61\u5141\u8bb8\u4f60\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u552f\u4e00\u503c\uff0c\u65e0\u8bba\u662f\u539f\u59cb\u503c\u6216\u8005\u662f\u5bf9\u8c61\u5f15\u7528\u3002<\/p>\n

    Set \u4e2d\u7684\u7279\u6b8a\u503c<\/strong><\/span><\/div>\n

    Set \u5bf9\u8c61\u5b58\u50a8\u7684\u503c\u603b\u662f\u552f\u4e00\u7684\uff0c\u6240\u4ee5\u9700\u8981\u5224\u65ad\u4e24\u4e2a\u503c\u662f\u5426\u6052\u7b49\u3002\u6709\u51e0\u4e2a\u7279\u6b8a\u503c\u9700\u8981\u7279\u6b8a\u5bf9\u5f85\uff1a<\/p>\n

      \n
    1. +0 \u4e0e -0 \u5728\u5b58\u50a8\u5224\u65ad\u552f\u4e00\u6027\u7684\u65f6\u5019\u662f\u6052\u7b49\u7684\uff0c\u6240\u4ee5\u4e0d\u91cd\u590d\uff1b<\/li>\n
    2. undefined \u4e0e undefined \u662f\u6052\u7b49\u7684\uff0c\u6240\u4ee5\u4e0d\u91cd\u590d\uff1b<\/li>\n
    3. NaN \u4e0e NaN \u662f\u4e0d\u6052\u7b49\u7684\uff0c\u4f46\u662f\u5728 Set \u4e2d\u53ea\u80fd\u5b58\u4e00\u4e2a\uff0c\u4e0d\u91cd\u590d\u3002<\/li>\n<\/ol>\n

      \u4ee3\u7801<\/strong><\/p>\n

      let mySet = new Set();\r\n \r\nmySet.add(1); \/\/ Set(1) {1}\r\nmySet.add(5); \/\/ Set(2) {1, 5}\r\nmySet.add(5); \/\/ Set(2) {1, 5} \u8fd9\u91cc\u4f53\u73b0\u4e86\u503c\u7684\u552f\u4e00\u6027\r\nmySet.add(\"some text\"); \r\n\/\/ Set(3) {1, 5, \"some text\"} \u8fd9\u91cc\u4f53\u73b0\u4e86\u7c7b\u578b\u7684\u591a\u6837\u6027\r\nvar o = {a: 1, b: 2}; \r\nmySet.add(o);\r\nmySet.add({a: 1, b: 2}); \r\n\/\/ Set(5) {1, 5, \"some text\", {\u2026}, {\u2026}} \r\n\/\/ \u8fd9\u91cc\u4f53\u73b0\u4e86\u5bf9\u8c61\u4e4b\u95f4\u5f15\u7528\u4e0d\u540c\u4e0d\u6052\u7b49\uff0c\u5373\u4f7f\u503c\u76f8\u540c\uff0cSet \u4e5f\u80fd\u5b58\u50a8<\/pre>\n
      \u7c7b\u578b\u8f6c\u6362<\/strong><\/span><\/div>\n

      Array<\/strong><\/p>\n

      \/\/ Array \u8f6c Set\r\nvar mySet = new Set([\"value1\", \"value2\", \"value3\"]);\r\n\/\/ \u7528...\u64cd\u4f5c\u7b26\uff0c\u5c06 Set \u8f6c Array\r\nvar myArray = [...mySet];\r\nString\r\n\/\/ String \u8f6c Set\r\nvar mySet = new Set('hello');  \/\/ Set(4) {\"h\", \"e\", \"l\", \"o\"}\r\n\/\/ \u6ce8\uff1aSet \u4e2d toString \u65b9\u6cd5\u662f\u4e0d\u80fd\u5c06 Set \u8f6c\u6362\u6210 String<\/pre>\n
      Set \u5bf9\u8c61\u4f5c\u7528<\/strong><\/span><\/div>\n

      \u6570\u7ec4\u53bb\u91cd<\/strong><\/p>\n

      var mySet = new Set([1, 2, 3, 4, 4]);\r\n[...mySet]; \/\/ [1, 2, 3, 4]<\/pre>\n

      \u5e76\u96c6<\/strong><\/p>\n

      var a = new Set([1, 2, 3]);\r\nvar b = new Set([4, 3, 2]);\r\nvar union = new Set([...a, ...b]); \/\/ {1, 2, 3, 4}<\/pre>\n

      \u4ea4\u96c6<\/strong><\/p>\n

      var a = new Set([1, 2, 3]);\r\nvar b = new Set([4, 3, 2]);\r\nvar intersect = new Set([...a].filter(x => b.has(x))); \/\/ {2, 3}<\/pre>\n

      \u5dee\u96c6<\/strong><\/p>\n

      var a = new Set([1, 2, 3]);\r\nvar b = new Set([4, 3, 2]);\r\nvar difference = new Set([...a].filter(x => !b.has(x))); \/\/ {1}<\/pre>\n","protected":false},"excerpt":{"rendered":"

      Map \u5bf9\u8c61\u4fdd\u5b58\u952e\u503c\u5bf9\u3002\u4efb\u4f55\u503c(\u5bf9\u8c61\u6216\u8005\u539f\u59cb\u503c) \u90fd\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u952e\u6216\u4e00\u4e2a\u503c\u3002 \u4e00\u4e2a Object \u7684\u952e\u53ea\u80fd\u662f\u5b57 […]<\/p>\n","protected":false},"author":321,"featured_media":199419,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-199415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=199415"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199415\/revisions"}],"predecessor-version":[{"id":199665,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199415\/revisions\/199665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/199419"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=199415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=199415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=199415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}