Keyed Collections

Maps : 

Map object : 

ECMA Script has introduced a new data structure from map values to values. A Map object contains a simple key/value map and can iterate in it's insertion order.

Example :

var sentences = new Map();

for this variable sentences we can set values i.e,

sentences.set("she" , "cute")
Here, she is a key and cute is a value.
sentences.set("He" , "back");
sentences.set("It" , "cool");

To know the size :

sentences.size; // 3

To get the value:

sentences.get('hello'); //undefined

sentences.get("she"); //cute

To delete a key/value pair :

sentences.delete("It");

To check whether the key/value pairs exists in the variable

sentences.has("He"); //  It returns a boolean value if the value exists i.e, true

sentences.has("the"); // false

Now retrieving the values :

for(var [key, value] of sentences){

       console.log(key + "is" + value);

}

output :

she is cute
He is back

To delete all the key/value pairs :

sentences.clear(); // all the key/value pairs are been deleted.

sentences.size; // 0

Comparison between Map and object

object are been used to map strings to values. It allows you to set keys to values, retrieve those values, delete those values etc. The iteration of maps is done in the insertion order of elements. The keys of an object are strings. Size of  map is easily known. As, it is shown in the above example. we have to keep track of the object to know the size manually. An object has a prototype and there are default keys in map.

WeakMap object :

It is a collection of key/value pairs in which keys will be objects only and values can be our choice.
The weakmap meaning is if there is no reference to the object, then it is targeted as a garbage collection. The difference between Map and WeakMap : It is not enumerable i.e, it has no methods to show the list of keys.

          WeakMap objects is stored as private. It is accessed only with in inside the object and are stored in private weakmap object.

Set object :

set object is collection of values. It can done in insertion order. A value in set can occur only once, i.e, it maintains only unique values.

var sets = new Set();

sets.add(100)
sets.add("hello");
sets.add("blogger");

sets.has(100); //true
sets.delete("hello"); //deleted
sets.has("hello"); // false
sets.size; //2

To retrieve the items from the sets variable :

for(let items in sets){

         console.log(items);   

}

output : 

//100
//blogger

Conversion of Array to set and vice-versa :

Arrays from set can be created i.e, through Array.form or the spread operator and also the set accepts to convert the array in other ways. As, set object only stores the unique values. So, any duplicate element from a array is deleted when converting it to the set.

Comparison between  Array and Set :
  • checking whether the elements exists in the array using indexOf for array is slow, whereas in set can be known using has().
  • set object let you to delete elements using their value whereas in array need to splice it based on it's element index.
  • set object stores unique values, no need check or keep track of duplicate values.
  • The value NaN cannot be found in indexOf in array.
WeaskSet objects :

Weakset objects are collection of objects and it may occur only once. It is unique in the weakset's collection and it is not enumerable.
If there is no other reference for the object in the weakset, then it is garbage collected.













No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...