Saturday 22 December 2018

JavaScript: Object vs Map

a. You can use any value as a key in map, but you can only use either strings (or) symbols as key in the object.

HelloWorld.js
var map = new Map();

var emp = {
  firstName : "Krishna",
  lastName : "Gurram"
}

map.set(1, "Number");
map.set(true, "Bollean");
map.set("String", "Hello");
map.set(1.23, "float");
map.set(emp, "Employee Object");

console.log(map)

Output
Map(5) { 1 "Number", true "Bollean", String "Hello", 1.23 "float", {} "Employee Object" }

b. Keys in the map are stored in insertion order, where as keys in the object are not.

c. Map provides useful method to get the size, values, keys, check for the existence of key etc.,

d. If you are going to perform multiple CRUD operations on an object, then map is better as compared to object.



Previous                                                 Next                                                 Home

No comments:

Post a Comment