Home  >  Article  >  Web Front-end  >  Detailed explanation of JavaScript data structure dictionary class

Detailed explanation of JavaScript data structure dictionary class

零到壹度
零到壹度Original
2018-04-09 11:29:122108browse

A dictionary is a data structure that stores data in the form of "key-value" pairs. Like names and numbers in a phone book. JavaScript's Object class is designed in the form of a dictionary.

1. Dictionary class

Dictionary class (Dictionary) is based on Object. In the book "Data Structure and Algorithm JavaScript Description", the "dictionary" uses array to store data, which not only makes it difficult for readers to understand, but also does not achieve convenience. On the contrary, the code logic is wrong. The results cannot be output correctly as designed! ! !

/**
 * 构造函数
 * 基于对象存储数据
 * @constructor
 */function Dictionary(){
    this.datastore = new Object();
}
Dictionary.prototype = {    /* 修正constructor */
    constructor: Dictionary,    /* 统计个数 */
    size: function(){
        return Object.keys(this.datastore).length;
    },    /* 添加元素,给数组添加属性 */
    add: function(key, value){
        this.datastore[key] = value;
    },    /* 查找指定key的元素 */
    find: function(key){
        return this.datastore[key];
    },    /* 移除指定key的元素 */
    remove: function(key){
        delete this.datastore[key];
    },    /* 显示所有的键值对 */
    showAll: function(){
        for(var key in this.datastore){
            console.log(key + ": " + this.find(key));
        }
    }
};

Test:

var dic = new Dictionary();
dic.add("name", "ligang");
dic.add("age", 26);
dic.find("name");   // "ligang"dic.size();         // 2dic.showAll();      // "name: ligang" "age: 26"dic.remove("age");
dic.size();         // 1dic.showAll();      // "name: ligang"

Supplementary:Object.keys(obj)Returns an array containing all (self ) enumerable properties. Please check out - JavaScript objects and functions (JavaScript you don’t know)

2. Add a sorting function to the dictionary class

Sorting the dictionary can be converted into sorting by an object attribute. So we can use Object.keys()

/* 排序 */Dictionary.prototype.sort = function(){
    // 借助数组的默认排序
    var keys = Object.keys(this.datastore).sort();    
    // 新定义字典类
    var tempDic = new Dictionary();    
    for(var i = 0, len = keys.length; i < len; i++){        var key = keys[i];
        tempDic.add(key, this.find(key));
    }    
    return tempDic;
};

Test:

var dictionary = new Dictionary();
dictionary.add("b", 2);
dictionary.add("a", 1);
dictionary.add("c", 3);
dictionary.showAll();   // "b: 2" "a: 1" "c: 3"dictionary.sort().showAll();    // "a: 2" "b: 1" "c: 3"

Summary: The above dictionary class is not allowed Duplicate keys appear. For the same key, the later one will overwrite the previous one. Of course, other ways can be achieved by modifying the code.

Related recommendations:

Dictionary type

Javascript dictionary operation

JS array, dictionary

The above is the detailed content of Detailed explanation of JavaScript data structure dictionary class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn