let nameObj = {
'Mary': 26,
'Tom': 13
};
console.log(nameObj)
//{Mary: 26, Tom: 13}
let nameMap = new Map();
nameMap.set('Mary', 26);
nameMap.set('Tom', 13);
console.log(nameMap);
// {'Mary' => 26, 'Tom' => 13}
console.log(nameMap.get('Mary')); //26
console.log(nameMap.has('Mary')); //true
console.log(nameMap.size); //2 similarly to array.length
console.log(nameMap.values()); // {26, 13}
console.log(nameMap.keys()); // {'Mary', 'Tom'}
console.log(nameMap.entries()); // {'Mary' => 26, 'Tom' => 13} [['Mary', 26], ['Tom', 13]]
// clone object
let nameObjClone = nameObj;
console.log(nameObj === nameObjClone); // true
// clone map
let nameMapClone = new Map(nameMap);
console.log(nameMap === nameMapClone); // false shallow copy
let nameMapClone2 = nameMap;
console.log(nameMap === nameMapClone2); // true
nameMap.set('Charles', {
apple: 1
})
let nameMapClone3 = new Map(nameMap);
console.log(nameMap.get('Charles') === nameMapClone3.get('Charles')); // true for shallow copy
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.