Javascript Data Structure Dictionary 3

Description

Javascript Data Structure Dictionary 3

var dictionary = new Dictionary();
dictionary.set("A", "a@email.com");
dictionary.set("B", "b@email.com");
dictionary.set("C", "c@email.com");

console.log(dictionary.has("A"));
console.log(dictionary.values());//from   ww  w  . j  a  v  a  2 s. c o  m


function Dictionary(){

    var items = {};

    this.set = function(key, value){

        items[key] = value;

    };

    this.remove = function(key){

        if(this.has(key)){
            delete items[key];
            return true;
        };

        return false;
    };

    this.has = function(key){

        return key in items;

    };

    this.get = function(key){

        return this.has(key)? items[key] : undefined;

    };

    this.clear = function(){
        this.items = {};
    };

    this.size = function(){
        this.items.length;
    };


    this.keys = function(){};



    this.values = function(){

        var values = [];

        for(var k in items){
            if(this.has(k)){
                values.push(items[k]);
            }
        }

        return values;

    };


    this.getItems = function(){
        return items;
    }

}

module.exports = Dictionary;



PreviousNext

Related