当前位置:网站首页>关于new Map( )还有哪些是你不知道的
关于new Map( )还有哪些是你不知道的
2022-07-05 16:08:00 【Fx_cap】
1、Map 的特点
- Map 对象保存键值对,并且能够记住键的原始插入顺序, 任何值都可以作为一个键或一个值
- Map 对象在迭代时会根据对象中的元素插入顺序来进行
- Map 对象使用 for ... of 循环在每次迭代后会返回一个形式为
[key, value]
的数组 - Map 对象键的比较基于零值相等的算法,在目前的 ECMAScript 规范中,
-0
和+0
被认为是相等的,使用 Object.is(-0,+0)返回 false - Map 对象设置属性时,以下代码可以正常运行,但是不推荐,因为这种设置属性的方式不会改变 Map 的数据结构,无法被查询到
const map = new Map(); map['bla'] = 'blaa'; console.log(map); // {bla: 'blaa', size: 0} console.log(map.has('bla')); //false console.log(map.get('bla')); //undefined console.log(map.delete('bla')); //false
- NaN 也可以作为
Map
对象的键const map = new Map(); map.set(NaN, 'not a number'); console.log(map); //{NaN => 'not a number'}
- 正确的存储数据到 Map 中的方式是使用
set(key, value)
方法const map = new Map(); map.set('bla', 'blaa'); console.log(map); //{'bla' => 'blaa'} console.log(map.has('bla')); //true console.log(map.get('bla')); //blaa console.log(map.delete('bla')); //true
2、Map 的实例属性
size: 返回 Map 对象中的键值对的个数
const map = new Map();
map.set('bla', 'blaa');
console.log(map.size); //1
3、Map 的实例方法:
- set ( key, value ) : 在
Map
对象中设置与指定的键key
关联的值value
,并返回Map
对象 - has( key ) : 返回一个布尔值,用来表明
Map
对象中是否存在与key
关联的值 - get ( key ) : 返回与
key
关联的值,若不存在关联的值,则返回undefined
- delete(key) : 移除
Map
对象中指定的键值对,如果键值对存在并成功被移除,返回true
,否则返回false
。调用delete
后再调用Map.prototype.has(key)
将返回false
clear():
移除Map
对象中所有的键值对
const map = new Map();
map.set('name', 'Andy'); // {'name' => 'Andy'}
map.set('age', 18); // {'name' => 'Andy'}
map.has('name'); //true
map.get('name'); //Andy
map.delete('name'); //true
map.set('name', 'Andy'); // {'name' => 'Andy'}
map.clear();
4、迭代方法
- keys( ) : 返回一个新的迭代对象,其中包含
Map
对象中所有的键,并以插入Map
对象的顺序排列 - values( ) : 返回一个新的迭代对象,其中包含
Map
对象中所有的值,并以插入Map
对象的顺序排列 - entries( ) : 返回一个新的迭代对象,其为一个包含
Map
对象中所有键值对的[key, value]
数组,并以插入Map
对象的顺序排列 - forEach( callback ) :
const map = new Map();
map.set('name', 'Andy'); // {'name' => 'Andy'}
map.set('age', 18); // {'name' => 'Andy'}
map.keys(); // {'name', 'age'}
map.values(); // {'Andy', 18}
map.entries(); // {'name' => 'Andy', 'age' => 18}
map.forEach((item) => {
console.log(item); //Andy 18
});
5、使用 for ... of 迭代
const map = new Map();
map.set('name', 'Andy');
map.set('age', 14);
for (const [key, value] of map) {
console.log(`key: ${key},value: ${value}`);
// key: name,value: Andy
// key: age,value: 14
}
for (const [key, value] of map.entries()) {
console.log(`key: ${key},value: ${value}`);
// key: name,value: Andy
// key: age,value: 14
}
for (const key of map.keys()) {
console.log(`key: ${key}`);
// key: name
// key: age
}
for (const value of map.values()) {
console.log(`value: ${value}`);
// value: Andy
// value: 14
}
6、Map 与数组的关系
- 使用常规的 Map 构造函数可以将一个二维键值对数组转成一个 Map 对象
const arr = [['name', 'Andy'], ['age', 14]]; const map = new Map(arr); console.log(map); // {'name' => 'Andy', 'age' => 14}
- 使用 Array.from 或展开运算符可以将一个Map对象转成一个二维数组
const map = new Map(); map.set('name', 'Andy'); map.set('age', 14); console.log([...map]);//[['name','Andy'],['age',14]]
7、复制或合并Map
- 复制
const map1 = new Map([['name', 'zs']]); const map2 = new Map(map1); console.log(map2 === map1);//false 浅比较 不为同一个对象的引用 const map1 = new Map([ ['name', 'zs'], ['hobby', { sing: 'xxxx' }], ]); const map2 = new Map(map1); for (const [key, value] of map1) { if (key === 'hobby') { value.sing = 'sub'; } } console.log(map1, map2);// hobby 引用同一地址
- Map 对象间合并,但是会保持键的唯一性,相同的键,后面的会把前面的值覆盖掉
const map1 = new Map([ ['name', 'zs'], ['age', 18], ]); const map2 = new Map([ ['name', 'ls'], ['age', 20], ]); const map3 = new Map([...map1, ...map2]); console.log(map3); // {'name' => 'ls', 'age' => 20}
- Map 对象与数组的合并,但是会保持键的唯一性,相同的键,后面的会把前面的值覆盖掉
const map1 = new Map([ ['name', 'zs'], ['age', 18], ]); const arr = ['name', 'ls']; const newMap = new Map([...map1, arr]); //{'name' => 'ls', 'age' => 18}
边栏推荐
- 利用GrayLog告警功能实现钉钉群机器人定时工作提醒
- Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
- Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
- Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
- 普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理
- Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
- 极坐标扇图使用场景与功能详解
- Data access - entityframework integration
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- 数据访问 - EntityFramework集成
猜你喜欢
Jarvis OJ Flag
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
ES6 drill down - Async functions and symbol types
极坐标扇图使用场景与功能详解
Research and development efficiency measurement index composition and efficiency measurement methodology
[deep learning] how does deep learning affect operations research?
OneForAll安装使用
Jarvis OJ Webshell分析
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
[brush questions] effective Sudoku
随机推荐
求解汉诺塔问题【修改版】
sqlserver 做cdc 要对数据库性能有什么要求么
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
[es6] 模板字符串内添加if判断或添加三元运算符判断
One click installation script enables rapid deployment of graylog server 4.2.10 stand-alone version
帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme
[js] skill simplification if empty judgment
清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
How can programmers improve their situation?
[deep learning] how does deep learning affect operations research?
《MongoDB入门教程》第04篇 MongoDB客户端
Raspberry pie 4B installation pytorch1.11
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
Domestic API management artifact used by the company
ES6 drill down - ES6 generator function
[js] 技巧 简化if 判空
Bs-xx-042 implementation of personnel management system based on SSM
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
Dare not buy thinking