map:类似与对象,key-value 对的方式存储
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
let m = new Map();
m.set("name","aaa");//设置
m.set("age",20);
console.log(m.get("name"));//读取
for (let [k,v] of m){
console.log(k+"=>"+v);
}
console.log(m.keys());
console.log(m.values());
console.log(m.entries());//整体输出
// 对象转为map
let u = {
name : "Lucy",
age : 20
};
let m1 = new Map();
for ( let i in u){
m1.set(i,u[i]);
}
console.log(m1);
//map=>对象
let u1 ={};
for ( let [k,v] of m1){
u1[k] = v;
}
console.log(u1);
</script>
</body>
</html>
网友评论