当前位置:网站首页>map结构
map结构
2022-07-27 05:03:00 【huijie_0716】
一、map对象的创建
var x=new Map();
var map1 = new Map([
['a', 1],
['b', 2],
['c', 1]
]);
二、 增加/添加
x.set(Symbol('a'),'张三');
x.set(Symbol('a'),'李四');
x.set('郑州','晴天');
x.set('开封','多云');
三、 修改
x.set('开封','晴天');
四、 查找
console.log(x.has('洛阳')); 返回true或false
五、删除
x.delete('开封');
x.clear();清空
六、遍历
x.forEach(function(val,key,map){ console.log(val,key) })
七、 keys /values /entries
for(let i of x.keys()){
console.log(i);
}
for(let i of x.values()){
console.log(i);
}
for(let i of x.entries()){
console.log(i);
}
for(let i of x){
console.log(i);
}八、 map转换key与值的关系
var map1 = new Map([
['a', 1],
['b', 2],
['c', 1]
]);
var map2 = new Map([
['郑州', '晴天'],
['开封', '多云'],
['东京', '海啸'],
['新乡', '海啸']
]);
function mapchenge(map) {
console.log(map);
let tem_map=new Map();//空map集合
for(let i of new Set(map.values())){//去重值后迭代
tem_map.set(i,[]);//添加数据到 map集合
}
//添加源值 到 新map
for(let [i,j] of map){
tem_map.get(j).push(i);
}
return tem_map;
}
var y = mapchenge(map1);
var x = mapchenge(map2);
console.log(y);
console.log(x);边栏推荐
- Li Hongyi machine learning team learning punch in activity day06 --- convolutional neural network
- Looking at the PK of alphago and Li Shishi from a deep perspective
- mysql 取消外键关联约束
- 后台品牌管理功能实现
- Redis cluster
- elment-ui使用方法
- C语言做一个小迷宫
- conda和pip环境常用命令
- 订单系统功能实现
- Source code of document type full-text retrieval knowledge base management system
猜你喜欢
随机推荐
Looking at the PK of alphago and Li Shishi from a deep perspective
flask一对多数据库创建,基础增删改查
torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()
Hi3516dv300 environment setup
Flask请求数据获取与响应
Flask登录实现
Li Hongyi machine learning team learning punch in activity day03 --- error and gradient decline
Time complexity and space complexity
李宏毅机器学习组队学习打卡活动day01---机器学习介绍
【codeforces 1695C Zero Path】DP
李宏毅机器学习组队学习打卡活动day03---误差和梯度下降
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
商品图片的管理
JDBC API details
自己动手做一个爬虫项目
后台用户管理展示添加功能实现
C语言初阶——分支语句(if,switch)
图片上传的逻辑
Niuke sword refers to the path in the offer--jz12 matrix
C语言入门介绍








