当前位置:网站首页>[JS -- map string]
[JS -- map string]
2022-07-02 04:02:00 【renrenrenrenqq】
Map
Map Object to save key value pairs , And remember the original insertion order of the keys . Any value ( Object or original value ) Can be used as a key or as a value .
Constructors
Map()
establish Map object
attribute
Map.length
attribute length The value of is 0 .
Want to calculate one Map Number of entries in , Use Map.prototype.size.
Example
1. Use Map object
let myMap = new Map();
let keyObj = {
};
let keyFunc = function() {
};
let keyString = 'a string';
// Add key
myMap.set(keyString, " Sum key 'a string' The value of the Association ");
myMap.set(keyObj, " Sum key keyObj The value of the Association ");
myMap.set(keyFunc, " Sum key keyFunc The value of the Association ");
myMap.size; // 3
// Read the values
myMap.get(keyString); // " Sum key 'a string' The value of the Association "
myMap.get(keyObj); // " Sum key keyObj The value of the Association "
myMap.get(keyFunc); // " Sum key keyFunc The value of the Association "
myMap.get('a string'); // " Sum key 'a string' The value of the Association "
// because keyString === 'a string'
myMap.get({
}); // undefined, because keyObj !== {}
myMap.get(function() {
}); // undefined, because keyFunc !== function () {}
2. take NaN As Map Key
NaN It can also be used as Map Object's key . although NaN Not equal to any value, not even yourself (NaN !== NaN return true), But the following example shows ,NaN As Map There is no difference for the key :
let myMap = new Map();
myMap.set(NaN, "not a number");
myMap.get(NaN); // "not a number"
let otherNaN = Number("foo");
myMap.get(otherNaN); // "not a number"
Use forEach() Method iteration Map
Map It can also be done through forEach() Method iteration :
let myMap = new Map();
myMap.set(0, "zero");
myMap.set(1, "one");
myMap.forEach(function(value, key) {
console.log(key + " = " + value);
})
// Two will be displayed logs. One is "0 = zero" The other is "1 = one"
Map Relationships to arrays
let kvArray = [["key1", "value1"], ["key2", "value2"]];
// Use regular Map The constructor can convert a two-dimensional array of key value pairs into a Map object
let myMap = new Map(kvArray);
myMap.get("key1"); // The return value is "value1"
// Use Array.from Function can Map Object to an array of two-dimensional key value pairs
console.log(Array.from(myMap)); // Output and kvArray Same array
// A simpler way to do the same thing as above , Use the expansion operator
console.log([...myMap]);
// Or use... On iterators of keys or values Array.from, Then get an array containing only keys or values
console.log(Array.from(myMap.keys())); // Output ["key1", "key2"]
Copy or merge Maps
Map Can be copied like an array :
let original = new Map([
[1, 'one']
]);
let clone = new Map(original);
console.log(clone.get(1)); // one
console.log(original === clone); // false. Shallow comparison Not a reference to the same object
Map Objects can be merged , But it will keep the uniqueness of the key .
let first = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
let second = new Map([
[1, 'uno'],
[2, 'dos']
]);
// Merge two Map Object time , If there are duplicate key values , Then the latter will cover the former .
// The expansion operator is essentially to expand Map Object to array .
let merged = new Map([...first, ...second]);
console.log(merged.get(1)); // uno
console.log(merged.get(2)); // dos
console.log(merged.get(3)); // three
Map Objects can also be merged with arrays :
let first = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
let second = new Map([
[1, 'uno'],
[2, 'dos']
]);
// Map Object is merged with an array , If there are duplicate key values , Then the latter will cover the former .
let merged = new Map([...first, ...second, [1, 'eins']]);
console.log(merged.get(1)); // eins
console.log(merged.get(2)); // dos
console.log(merged.get(3)); // three
边栏推荐
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- [Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
- JVM knowledge points
- go 包的使用
- Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
- Jetpack之LiveData扩展MediatorLiveData
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝湖的安装及使用
- pip 安装第三方库
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
猜你喜欢
![[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)](/img/5e/81e613370c808c63665c14298f9a39.png)
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)

NLog use
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/e3/fc2e78dc1e3e3cacbd1a389c82d33e.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

The 5th Blue Bridge Cup single chip microcomputer provincial competition

手撕——排序

整理了一份ECS夏日省钱秘籍,这次@老用户快来领走

《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮

How much is the tuition fee of SCM training class? How long is the study time?

Influence of air resistance on the trajectory of table tennis
![[untitled]](/img/53/cb61622cfcc73a347d2d5e852a5421.jpg)
[untitled]
随机推荐
[tips] use Matlab GUI to read files in dialog mode
蓝桥杯单片机省赛第六届
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
QT designer plug-in implementation of QT plug-in
Hands on deep learning (II) -- multi layer perceptron
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Interface debugging tool simulates post upload file - apipost
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
Is the product of cancer prevention medical insurance safe?
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
【IBDFE】基于IBDFE的频域均衡matlab仿真
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Www 2022 | rethinking the knowledge map completion of graph convolution network
Go function
蓝湖的安装及使用
接口调试工具模拟Post上传文件——ApiPost
蓝桥杯单片机第六届温度记录器
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
蓝桥杯单片机数码管技巧