导航
var m = {};
var x = {id: 1},
y = {id: 2},
z = [1, 2, 3];
m[x] = 'foo';
m[y] = 'bar';
m[z] = 'arr';
console.log(m[x]);
console.log(m[y]);
console.log(m[z]);
console.log(m);
var m = new Map([['name', 'zhangsan'], ['age', 10]]);
console.log(m);
var map = new Map();
map.set([5], 555);
console.log(map.get([5])); // undefined 数组是个引用
var map = new Map();
var arr = [5];
map.set(arr, 555);
console.log(map.get(arr));
var map = new Map();
map.set(-0, 123);
console.log(map.get(+0))
console.log(+0 === -0);
console.log(Object.is(+0, -0));
var map = new Map();
map.set(undefined, 1);
map.set(null, 2);
map.set(NaN, 3);
console.log(map);
console.log(map.get(undefined));
console.log(map.get(null));
console.log(map.get(NaN));
var map = new Map();
console.log(map);
var m = new Map();
var x = {id: 1},
y = {id: 2};
m.set(x, 'foo');
m.set(y, 'bar'); // 返回值是map结构,也可以进行链式调用
console.log(m.size); // 2
console.log(m.delete(x)); // 返回布尔值
console.log(m.clear()); // 返回undefined
const map = new Map([
['name', 'Lance'],
['age', 26]
]);
map.set('weight', 135);
const arr = [...map];
console.log(map);
console.log(arr);
const arr = [
['name', 'Lance'],
['age', 26],
['weight', 135]
]
const map = new Map(arr);
console.log(map);
const map = new Map([
['name', 'Lance'],
['age', 26]
]);
const person = {};
function strMapToObj(map) {
let obj = Object.create(null); // 纯净对象,无原型
for ([key, value] of map) {
obj[key] = value;
}
return obj;
}
console.log(strMapToObj(map));
const person = {
name: 'Lance',
age: 26
}
function objToMap(obj) {
let map = new Map();
for (const [key, value] of Object.entries(obj)) {
map.set(key, value);
}
return map;
}
console.log(objToMap(person));