当前位置:网站首页>关于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}
边栏推荐
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- 效果编辑器新版上线!3D渲染、加标注、设置动画,这次一个编辑器就够了
- Is it safe for Guotai Junan to open an account online
- Benji Banas membership pass holders' second quarter reward activities update list
- Seaborn绘制11个柱状图
- Facing new challenges and becoming a better self -- attacking technology er
- 单商户 V4.4,初心未变,实力依旧!
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- Sentinel-流量防卫兵
- Enter a command with the keyboard
猜你喜欢

How does win11 change icons for applications? Win11 method of changing icons for applications
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存

OneForAll安装使用

迁移/home分区

If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats

Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)

Enter a command with the keyboard

Get ready for the pre-season card game MotoGP ignition champions!

File operation --i/o

Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
随机推荐
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
scratch五彩糖葫芦 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
Cartoon: what is distributed transaction?
The difference between searching forward index and inverted index
Mongodb getting started Tutorial Part 04 mongodb client
Android privacy sandbox developer preview 3: privacy, security and personalized experience
How to use FRP intranet penetration +teamviewer to quickly connect to the intranet host at home when mobile office
File operation --i/o
[brush questions] effective Sudoku
[brush title] goose factory shirt problem
企业级备份软件Veritas NetBackup(NBU) 8.1.1服务端的安装部署
Enterprise backup software Veritas NetBackup (NBU) 8.1.1 installation and deployment of server
新春限定丨“牛年忘烦”礼包等你来领~
[deep learning] how does deep learning affect operations research?
"21 days proficient in typescript-3" - install and build a typescript development environment md
Using graylog alarm function to realize the regular work reminder of nail group robots
【刷题篇】鹅厂文化衫问题
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
APICloud云调试解决方案