当前位置:网站首页>[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
边栏推荐
猜你喜欢
手撕——排序
Raspberry pie GPIO pin controls traffic light and buzzer
Installation et utilisation du lac bleu
Go language introduction
Failed to upgrade schema, error: “file does not exist
《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
蓝桥杯单片机省赛第十一届第一场
C language: examples of logical operation and judgment selection structure
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
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
随机推荐
PR zero foundation introductory guide note 2
Vite: configure IP access
Li Kou interview question 02.08 Loop detection
Target free or target specific: a simple and effective zero sample position detection comparative learning method
The 10th Blue Bridge Cup single chip microcomputer provincial competition
蓝桥杯单片机数码管技巧
Interface debugging tool simulates post upload file - apipost
Www 2022 | rethinking the knowledge map completion of graph convolution network
C语言:逻辑运算和判断选择结构例题
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
Hand tear - sort
5G时代全面到来,浅谈移动通信的前世今生
The original author is out! Faker. JS has been controlled by the community..
Fingertips life Chapter 4 modules and packages
Class design basis and advanced
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
Didi open source Delta: AI developers can easily train natural language models
蓝桥杯单片机省赛第五届
接口调试工具模拟Post上传文件——ApiPost
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证