当前位置:网站首页>[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
边栏推荐
- SQL: common SQL commands
- The 11th Blue Bridge Cup single chip microcomputer provincial competition
- Wpviewpdf Delphi and Net PDF viewing component
- 蓝桥杯单片机省赛第九届
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板,老板也有建设积分和捣乱积分, 排好队后,所有
- L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- The confusion I encountered when learning stm32
- LxC limits the number of CPUs
猜你喜欢

go 包的使用

How should the team choose the feature branch development mode or trunk development mode?

跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间

PIP installation of third-party libraries

SQL Yiwen get window function

Opencv learning example code 3.2.4 LUT

The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

Learn more about materialapp and common attribute parsing in fluent

软件测试人的第一个实战项目:web端(视频教程+文档+用例库)

MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
随机推荐
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
How to solve the code error when storing array data into the database
蓝桥杯单片机省赛第八届
[Li Kou brush questions] 15 Sum of three numbers (double pointer); 17. Letter combination of phone number (recursive backtracking)
整理了一份ECS夏日省钱秘籍,这次@老用户快来领走
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
蓝桥杯单片机省赛第九届
Influence of air resistance on the trajectory of table tennis
Didi open source Delta: AI developers can easily train natural language models
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
5G時代全面到來,淺談移動通信的前世今生
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
蓝湖的安装及使用
Is the product of cancer prevention medical insurance safe?
SQL: common SQL commands
初识string+简单用法(二)
向数据库中存入数组数据,代码出错怎么解决
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
Visual slam Lecture 3 -- Lie groups and Lie Algebras