当前位置:网站首页>[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
边栏推荐
- Object oriented thinking
- 蓝桥杯单片机省赛第九届
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
- Raspberry pie GPIO pin controls traffic light and buzzer
- Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
- 蓝桥杯单片机省赛第七届
- 初识string+简单用法(二)
- Vite: configure IP access
- 毕设-基于SSM电影院购票系统
猜你喜欢
C语言:逻辑运算和判断选择结构例题
Li Kou interview question 02.08 Loop detection
MD5 of Oracle
蓝桥杯单片机第六届温度记录器
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
蓝桥杯单片机省赛第六届
[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!
蓝桥杯单片机省赛第七届
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
【无线图传】基于FPGA的简易无线图像传输系统verilog开发,matlab辅助验证
随机推荐
【leetcode】34. Find the first and last positions of elements in a sorted array
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
QT designer plug-in implementation of QT plug-in
Opencv learning example code 3.2.4 LUT
How to solve the code error when storing array data into the database
First acquaintance with P4 language
First acquaintance with string+ simple usage (II)
Installation et utilisation du lac bleu
Wpviewpdf Delphi and Net PDF viewing component
Go language naming specification
NLog use
Go语言介绍
Where can I buy cancer insurance? Which product is better?
Fluent icon demo
Www 2022 | rethinking the knowledge map completion of graph convolution network
SQL:常用的 SQL 命令
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
JVM knowledge points