Guest User

Untitled

a guest
Nov 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. // Map Example
  2.  
  3. const a = new Map();
  4. const b = {};
  5.  
  6. //Adding key-value pairs
  7. console.log("Set Key");
  8. //Map
  9. a.set('test', 3);
  10. console.log(...a.entries()); //[ 'test', 3 ]
  11. //Object
  12. b['test'] = 3;
  13. console.log(b); //{ test: 3 }
  14. //Get key
  15. console.log("\nGet Key");
  16. //Map
  17. const c = a.get('test');
  18. console.log("Map:", c);//Map: 3
  19. //Object
  20. const d = b['test'];
  21. console.log("Object:", d); //Object: 3
  22.  
  23.  
  24. //Has key
  25. console.log("\nHas Test");
  26. //Map
  27. console.log("Map:", a.has('test')); //true
  28.  
  29. //Object
  30. console.log("Object:", b['test'] !== undefined); //true (But could be set to null or undefined by developer)
  31.  
  32. //Remove key
  33. console.log("\nDelete Key");
  34. //Map
  35. console.log("Map:", a.delete('test')); //true if there is a key to remove
  36. console.log("Map:", a.get('test')); //Map: undefined
  37. //Object
  38. delete b['test'];
  39. console.log("Object:",b['test']); //Object: undefined
Add Comment
Please, Sign In to add comment