当前位置:网站首页>What else do you not know about new map()
What else do you not know about new map()
2022-07-05 17:00:00 【Fx_ cap】
1、Map Characteristics
- Map Object to save key value pairs , And remember the original insertion order of the keys , Any value can be a key or a value
- Map Objects will be iterated according to the insertion order of elements in the object
- Map Object use for ... of After each iteration, the loop returns a value of the form
[key, value]
Array of - Map The comparison of object keys is based on Zero equal The algorithm of , At present ECMAScript Specification ,
-0
and+0
Considered equal , Use Object.is(-0,+0) return false - Map Object setting properties , The following code works correctly , But not recommended , Because this way of setting attributes will not change Map Data structure of , Cannot be queried
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 It can also be used as
Map
Object's keyconst map = new Map(); map.set(NaN, 'not a number'); console.log(map); //{NaN => 'not a number'}
- Store data correctly to Map The way in is to use
set(key, value)
Methodconst 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 Instance properties for
size: return Map Number of key value pairs in the object
const map = new Map();
map.set('bla', 'blaa');
console.log(map.size); //1
3、Map Instance method of :
- set ( key, value ) : stay
Map
The key set and specified in the objectkey
The value of the Associationvalue
, And back toMap
object - has( key ) : Returns a Boolean value , Used to indicate
Map
Whether there is a connection withkey
The value of the Association - get ( key ) : Return and
key
The value of the Association , If there is no associated value , Then return toundefined
- delete(key) : remove
Map
Specifies the key value in the object pair , If the key value pair exists and is successfully removed , returntrue
, Otherwise return tofalse
. calldelete
Then callMap.prototype.has(key)
Will returnfalse
clear():
removeMap
All key value pairs in the object
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、 Iterative method
- keys( ) : Returns a new iteration object , It includes
Map
All of the objects key , And insertMap
The order of objects - values( ) : Returns a new iteration object , It includes
Map
All of the objects value , And insertMap
The order of objects - entries( ) : Returns a new iteration object , It is a containing
Map
Of all key value pairs in the object[key, value]
Array , And insertMap
The order of objects - 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、 Use for ... of iteration
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 Relationships to arrays
- Use regular Map Constructor can convert a two-dimensional array of key value pairs into a Map object
const arr = [['name', 'Andy'], ['age', 14]]; const map = new Map(arr); console.log(map); // {'name' => 'Andy', 'age' => 14}
- Use Array.from Or the expansion operator can put a Map Object into a two-dimensional array
const map = new Map(); map.set('name', 'Andy'); map.set('age', 14); console.log([...map]);//[['name','Andy'],['age',14]]
7、 Copy or merge Map
- Copy
const map1 = new Map([['name', 'zs']]); const map2 = new Map(map1); console.log(map2 === map1);//false Shallow comparison Not a reference to the same object 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 Reference the same address
- Map Merge between objects , But it will keep the uniqueness of the key , The same key , The latter will overwrite the previous value
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 Object and array merge , But it will keep the uniqueness of the key , The same key , The latter will overwrite the previous value
const map1 = new Map([ ['name', 'zs'], ['age', 18], ]); const arr = ['name', 'ls']; const newMap = new Map([...map1, arr]); //{'name' => 'ls', 'age' => 18}
边栏推荐
- Data verification before and after JSON to map -- custom UDF
- 数据访问 - EntityFramework集成
- Benji Banas membership pass holders' second quarter reward activities update list
- Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
- Hiengine: comparable to the local cloud native memory database engine
- Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
- 养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
- Jarvis OJ Flag
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- PHP人才招聘系统开发 源代码 招聘网站源码二次开发
猜你喜欢
Solution of vant tabbar blocking content
Jarvis OJ 远程登录协议
Hiengine: comparable to the local cloud native memory database engine
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
WSL2.0安装
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
浏览器渲染原理以及重排与重绘
Benji Banas membership pass holders' second quarter reward activities update list
[Jianzhi offer] 63 Maximum profit of stock
Browser rendering principle and rearrangement and redrawing
随机推荐
Facing new challenges and becoming a better self -- attacking technology er
JSON转MAP前后数据校验 -- 自定义UDF
npm安装
How was the middle table destroyed?
Wsl2.0 installation
Combined use of vant popup+ other components and pit avoidance Guide
深潜Kotlin协程(二十一):Flow 生命周期函数
PHP strict mode
Android privacy sandbox developer preview 3: privacy, security and personalized experience
Deeply cultivate 5g, and smart core continues to promote 5g applications
[Jianzhi offer] 61 Shunzi in playing cards
Summary of methods for finding intersection of ordered linked list sets
Starkware: to build ZK "universe"
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
【微信小程序】一文读懂小程序的生命周期和路由跳转
Timestamp strtotime the day before or after the date
Deep dive kotlin synergy (XXI): flow life cycle function
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
采用药丸屏的iPhone14或引发中国消费者的热烈抢购