• 关于new Map( )还有哪些是你不知道的


    1、Map 的特点

    • Map 对象保存键值对,并且能够记住键的原始插入顺序, 任何值都可以作为一个键或一个值
    • Map 对象在迭代时会根据对象中的元素插入顺序来进行
    • Map 对象使用 for ... of  循环在每次迭代后会返回一个形式为 [key, value] 的数组
    • Map 对象键的比较基于零值相等的算法,在目前的 ECMAScript 规范中,-0 和 +0 被认为是相等的,使用 Object.is(-0,+0)返回 false
    •  Map 对象设置属性时,以下代码可以正常运行,但是不推荐,因为这种设置属性的方式不会改变 Map 的数据结构,无法被查询到
      1. const map = new Map();
      2. map['bla'] = 'blaa';
      3. console.log(map); // {bla: 'blaa', size: 0}
      4. console.log(map.has('bla')); //false
      5. console.log(map.get('bla')); //undefined
      6. console.log(map.delete('bla')); //false
    • NaN 也可以作为 Map 对象的键
      1. const map = new Map();
      2. map.set(NaN, 'not a number');
      3. console.log(map); //{NaN => 'not a number'}
    • 正确的存储数据到 Map 中的方式是使用 set(key, value) 方法
      1. const map = new Map();
      2. map.set('bla', 'blaa');
      3. console.log(map); //{'bla' => 'blaa'}
      4. console.log(map.has('bla')); //true
      5. console.log(map.get('bla')); //blaa
      6. console.log(map.delete('bla')); //true

    2、Map 的实例属性 

    size: 返回 Map 对象中的键值对的个数

    1. const map = new Map();
    2. map.set('bla', 'blaa');
    3. console.log(map.size); //1

    3、Map 的实例方法:

    1. set ( key, value ) : 在 Map 对象中设置与指定的键 key 关联的值 value,并返回 Map 对象
    2. has( key ) : 返回一个布尔值,用来表明 Map 对象中是否存在与 key 关联的值
    3. get ( key ) : 返回与 key 关联的值,若不存在关联的值,则返回 undefined
    4. delete(key) : 移除 Map 对象中指定的键值对,如果键值对存在并成功被移除,返回 true,否则返回 false。调用 delete 后再调用 Map.prototype.has(key) 将返回 false
    5. clear(): 移除 Map 对象中所有的键值对
    1. const map = new Map();
    2. map.set('name', 'Andy'); // {'name' => 'Andy'}
    3. map.set('age', 18); // {'name' => 'Andy'}
    4. map.has('name'); //true
    5. map.get('name'); //Andy
    6. map.delete('name'); //true
    7. map.set('name', 'Andy'); // {'name' => 'Andy'}
    8. map.clear();

     4、迭代方法

    1. keys( ) : 返回一个新的迭代对象,其中包含 Map 对象中所有的,并以插入 Map 对象的顺序排列
    2. values( ) : 返回一个新的迭代对象,其中包含 Map 对象中所有的,并以插入 Map 对象的顺序排列
    3. entries( ) : 返回一个新的迭代对象,其为一个包含 Map 对象中所有键值对的 [key, value] 数组,并以插入 Map 对象的顺序排列
    4. forEach( callback ) : 
    1. const map = new Map();
    2. map.set('name', 'Andy'); // {'name' => 'Andy'}
    3. map.set('age', 18); // {'name' => 'Andy'}
    4. map.keys(); // {'name', 'age'}
    5. map.values(); // {'Andy', 18}
    6. map.entries(); // {'name' => 'Andy', 'age' => 18}
    7. map.forEach((item) => {
    8. console.log(item); //Andy 18
    9. });

    5、使用 for ... of 迭代

    1. const map = new Map();
    2. map.set('name', 'Andy');
    3. map.set('age', 14);
    4. for (const [key, value] of map) {
    5. console.log(`key: ${key},value: ${value}`);
    6. // key: name,value: Andy
    7. // key: age,value: 14
    8. }
    9. for (const [key, value] of map.entries()) {
    10. console.log(`key: ${key},value: ${value}`);
    11. // key: name,value: Andy
    12. // key: age,value: 14
    13. }
    14. for (const key of map.keys()) {
    15. console.log(`key: ${key}`);
    16. // key: name
    17. // key: age
    18. }
    19. for (const value of map.values()) {
    20. console.log(`value: ${value}`);
    21. // value: Andy
    22. // value: 14
    23. }

    6、Map 与数组的关系

    1. 使用常规的 Map 构造函数可以将一个二维键值对数组转成一个 Map 对象
      1. const arr = [['name', 'Andy'], ['age', 14]];
      2. const map = new Map(arr);
      3. console.log(map); // {'name' => 'Andy', 'age' => 14}
    2. 使用 Array.from 或展开运算符可以将一个Map对象转成一个二维数组
      1. const map = new Map();
      2. map.set('name', 'Andy');
      3. map.set('age', 14);
      4. console.log([...map]);//[['name','Andy'],['age',14]]

    7、复制或合并Map 

    1. 复制
      1. const map1 = new Map([['name', 'zs']]);
      2. const map2 = new Map(map1);
      3. console.log(map2 === map1);//false 浅比较 不为同一个对象的引用
      4. const map1 = new Map([
      5. ['name', 'zs'],
      6. ['hobby', { sing: 'xxxx' }],
      7. ]);
      8. const map2 = new Map(map1);
      9. for (const [key, value] of map1) {
      10. if (key === 'hobby') {
      11. value.sing = 'sub';
      12. }
      13. }
      14. console.log(map1, map2);// hobby 引用同一地址
    2. Map 对象间合并,但是会保持键的唯一性,相同的键,后面的会把前面的值覆盖掉
      1. const map1 = new Map([
      2. ['name', 'zs'],
      3. ['age', 18],
      4. ]);
      5. const map2 = new Map([
      6. ['name', 'ls'],
      7. ['age', 20],
      8. ]);
      9. const map3 = new Map([...map1, ...map2]);
      10. console.log(map3); // {'name' => 'ls', 'age' => 20}
    3. Map 对象与数组的合并,但是会保持键的唯一性,相同的键,后面的会把前面的值覆盖掉
      1. const map1 = new Map([
      2. ['name', 'zs'],
      3. ['age', 18],
      4. ]);
      5. const arr = ['name', 'ls'];
      6. const newMap = new Map([...map1, arr]); //{'name' => 'ls', 'age' => 18}

     

  • 相关阅读:
    好工具分享:阿里云价格计算器_一键计算精准报价
    Rust 深度学习库 Burn
    JavaFX、合并碰撞的弹球
    vue项目开发环境工具-node
    开源轻量级工作流WorkflowCore介绍
    Visual Studio 2022新建项目时没有ASP.NET项目
    ubuntu20.04安装genymotion3.5.1
    《网络安全笔记》第八章:计算机网络基本概念
    JDBC和数据连接池
    LeetCode【128】最长连续序列
  • 原文地址:https://blog.csdn.net/m0_56274171/article/details/125595896