当前位置:网站首页>关于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); //13、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}
边栏推荐
- 践行自主可控3.0,真正开创中国人自己的开源事业
- 二叉树相关OJ题
- Oneforall installation and use
- 树莓派4b安装Pytorch1.11
- 一些认知的思考
- CISP-PTE之SQL注入(二次注入的应用)
- Pspnet | semantic segmentation and scene analysis
- 效果编辑器新版上线!3D渲染、加标注、设置动画,这次一个编辑器就够了
- [es6] add if judgment or ternary operator judgment in the template string
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
猜你喜欢

【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……

Single merchant v4.4 has the same original intention and strength!

怎样在电脑上设置路由器的WiFi密码

Seaborn draws 11 histograms

BS-XX-042 基于SSM实现人事管理系统

The database of the server is not connected to 200310060 "unknown error" [the service is up, the firewall is off, the port is on, and the netlent port is not connected]

Bs-xx-042 implementation of personnel management system based on SSM

二叉树相关OJ题

极坐标扇图使用场景与功能详解

Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
随机推荐
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
程序员如何提升自己的格局?
Reduce the cost by 40%! Container practice of redis multi tenant cluster
PSPNet | 语义分割及场景分析
中间表是如何被消灭的?
tf.sequence_mask函数讲解案例
Starkware: to build ZK "universe"
How to set the WiFi password of the router on the computer
有序链表集合求交集 方法 总结
[deep learning] [original] let yolov6-0.1.0 support the txt reading dataset mode of yolov5
Intel 13th generation Raptor Lake processor information exposure: more cores, larger cache
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
2020-2022 two-year anniversary of creation
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
[深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式
The memory of a Zhang
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
记一次'非常诡异'的云安全组规则问题排查过程
ES6 drill down - ES6 generator function
Accès aux données - intégration du cadre d'entité