ES2015 OR ES6 Sets

Set Objects are collection of values where the values can be iterated through insertion order and it doesn't accept duplicate values i.e. it contains only unique values of any type i.e. primitive type or object references.

Syntax :

new Set([iterable]);

iterable parameter will add all the elements into the new Set. If the parameter is not set or the value is null then it returns a new empty Set.

NaN and undefined can also be stored in the set.

Properties :

Set.length  : By default the length is 0 and it returns the length of the set object.

Set.prototype : It allows the addition of properties to all the Set Objects and it represents the prototype of the set constructor.

Set instances :

All the Set instances are been inherited from Set.prototype.

Properties :

Set.prototype.size : 

It returns the length of the Set Object.

Set.prototype.constructor : 

It returns a function that is been created as the instance prototype and  this will be Set function by default.

Methods :

Set.prototype.add(val) : 

It will append the new element to the Set Object and then returns the Set Object.

Set.prototype.clear() : 

It removes all the elements from the Set Object.

Set.prototype.delete(val) : 

It removes the specific element from the Set Object and through has() method we can check whether the element exits or not it returns in boolean format i.e. true if the element exits else it returns false.

Set.prototype.has(value) : 

It returns boolean value i.e. if the element is present in the given set object then it returns true else it returns false.

Set.prototype.entries() : 

It returns a new Iterator object that returns an array [value,value] for each element in the set object in the insertion order.

Set.prototype.forEach(callbackfunction[,thisArg]) :  

The callback function will call for each and every value present in the Set Object in the insertion order and thisArg is been passed to the forEach, it is used as a this value for every calback.

Set.prototype.keys() :

It is same as values() method. It returns a new Iterator object that has values of each element in the Set Object in the insertion order.

Set.prototype.values() :

It returns a new Iterator object that has values of each element in the Set Object in the insertion order.

Example for the above methods :

var myNewSet = new Set();
myNewSet.add(1); // [1]
myNewSet.add(2);  // [1,2]
myNewSet.add(2); // [1,2]
myNewSet.add('text'); // [1,2,text]

myNewSet.size; //3

myNewSet.has(Math.sqrt(1)); //true

myNewSet.add(10); // [1,2,text,10]

myNewSet.has(Math.sqrt(100)); //true

var emp = { name : "sita",age : "20" };

myNewSet.add(emp);

myNewSet.has(1); // true

myNewSet.has('text'); //true

myNewSet.delete(2); // removes 2 from the set

myNewSet.has(2); //false

myNewSet.size; // 4

Iterating Sets :

for(let item of myNewSet) { console.log(item) } 

output :

1
text
10
{name: "sita", age: "20"}

for(let item of myNewSet.keys()) { console.log(item) } 

1
text
10
{name: "sita", age: "20"}

for(let item of myNewSet.values()) { console.log(item) } 
1
text
10
{name: "sita", age: "20"}

for(let item of myNewSet.entries()) { console.log(item) } 
[1, 1]
["text", "text"]
[10, 10]
[{name:"sita",age:"20"}]

var myArr = Array.from(myNewSet);
console.log(myArr);
output :

[1,"text",10, { name:"sita",age : "20" } ]

myNewSet.forEach(function(value) {
  console.log(value);
});

output :

1
text
10
{name:"sita",age:"20"}

Basic set operations :

1. union :

Set.prototype.union = function(setB) {
    var union = new Set(this);
    for (var elem of setB) {
        union.add(elem);
    }
    return union;
}

var setA = new Set([1,2,3,4]);
var setB = new Set([1,2,5,6]);
setA.union(setB); // [1,2,3,4,5,6]

2. Intersection :

Set.prototype.intersection = function(setB) {
    var intersection = new Set();
    for (var elem of setB) {
        if (this.has(elem)) {
            intersection.add(elem);
        }
    }
    return intersection;
}
var setA = new Set([1,2,3,4]);
var setB = new Set([1,2,5,6]);
setA.intersection(setB); // [1,2]

3. difference :
 Set.prototype.difference = function(setB) {
    var difference = new Set(this);
    for (var elem of setB) {
        difference.delete(elem);
    }
    return difference;
}

var setA = new Set([1,2,3,4]);
var setB = new Set([1,2,5,6]);

setA.difference(setB); // [3,4]

setB.difference(setA); [5,6]

Relation with Strings :

var text = "Blogger";

var myNewSet = new Set(text);

console.log(myNewSet);

["B","l","o","g","g","e","r"]

myNewSet .size; //6

Relation with Arrays :

var myArr = ['val1','val2','val3'];

var myNewSet = new Set(myArr);

myNewSet .has('val1'); //true

To transform a set into an Array we use spread operator.

console.log([...myNewSet]); //["val1", "val2", "val3"]

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...