My Simple Learning of JS Collections

My Learning of Collection

The first step is to know the relevant concepts.

Sets are composed of a set of disorderly and unique items, and similar Set classes have been implemented in ES6.

function Set() {
    var items = {}; //items are collections
}

The second step is to realize the relevant operation methods.

First, the has(value) method is implemented to determine whether the value is in the set and return the Boolean value. Because collections do not allow duplicate elements to exist, other methods call this method to determine whether the value already exists.

this.has = function(value) {
    return value in items; //in operator
};

There is another implementation, the hasOwnProperty() method returns a Boolean value indicating whether an object has a specific property.

this.has = function(value) {
    return items.hasOwnProperty(value);
};

Next, we implement the add() method to add a new item to the collection.

this.add = function(value) {
    if(!this.has(value)) { //Determine whether the element to be added already exists
        items[value] = value; //Note that saving as keys and values at the same time helps to find this value
        return true;
    }
    return false; //The added element already exists and is no longer added
};

Next, the remove() method is implemented to remove a value from the collection.

this.remove = function(value) {
    if(this.has(value)) {
        delete items[value]; //Delete attributes of objects
        return true;
    }
    return false;
};

Next, the clear() method is implemented to remove all the values of the collection.

this.clear = function() {
    items = {}; //Empty objects are reassigned to it
};

Next, implement the size() method to return how many items are in the collection.

  • The first implementation uses a length variable

  • The second implementation method uses Object.keys()

this.size = function() {
    return Object.keys(items).length; //Returns an array of all enumerable attribute names of an object, but does not include the attributes in the prototype, nor does it guarantee that the attribute names are output sequentially.
};
  • The third method is to extract each attribute of items object manually and record the number of items.

this.size = function() {
    var count = 0;
    for(var prop in items) { //Traversing through all attributes of items
        if(items.hasOwnProperty(prop)) { //Prevent counting attributes from being computed to the prototype
            ++count;
        }
    }
    return count;
};

Implement the values() method, returning an array of all values.

this.values = function() {
    return Object.keys(items); //Get the key and get the value (because they are the same).
};

The third step is to simply use the Set class

var set = new Set();
set.add(1); 
console.log(set.values()); //['1']
console.log(set.has(1)); //true
console.log(set.size()); //1

set.add(2);
console.log(set.values()); //['1', '2']

set.remove(1); 
console.log(set.values()); //['2']

Step 4 Operational Set

  • Unification: Two sets, returning a new set containing all elements in two sets

It can be used to merge two elements, and it ensures singularity.

this.union = function(otherSet) {
    var unionSet = new Set(); //Union result
    var values = this.values();
    for(var i = 0; i < values.length; i++) { //Traversing through the first set and putting it all into the new set
        unionSet.add(values[i]);
    }
    values = otherSet.values();
    for(var i = 0; i < values.length; i++) { //Traveling through the second set and putting it all into the new set ensures the singularity by using the add method
        unionSet.add(values[i]);
    }
    return unionSet;
}
  • Intersection: Two sets, returning a new set containing common elements in two sets

It can be used to take the common part of both.

this.intersection = function(otherSet) {
    var intersectionSet = new Set();
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        if(otherSet.has(values[i])) {
            intersectionSet.add(values[i])    
        }
    }
    return intersectionSet;
}
  • Difference Set: Elements exist in A and do not exist in B

this.difference = functiong(otherSet) {
    var differenceSet = new Set();
    var values = this.values();
    for(var i = 0; i < values.length; i++) { //Traveling through A
        if(!otherSet.has(values[i])) { 
            differenceSet.add(values[i]);
        }
    }
    return differenceSet;
}
  • Subset: Every element in A is in B

this.subset = function(otherSet) {
    if(this.size() > otherSet.size()) return false;
    var values = this.values();
    for(var i = 0; i < values.length; i++) {
        if(!otherSet.has(values[i])) { //As long as one element in A is not in B, return false
            return false;    
        }
    }
    return true;
}

Keywords: Javascript Attribute

Added by Plasma on Tue, 16 Jul 2019 22:35:48 +0300