第六章:javascript:字典


字典是一種以鍵-值對應形式存儲的數據結構,就像電話薄里的名字和電話號碼一樣。只要找一個電話,查找名字,名字找到后,電話號碼也就找到了。這里的鍵值是你用來查找的東西,值就是要查的到的結果。

javascript的Object類就是以這種字典的形式設計的。本章利用Object類本身的特性,實現一個Dictionary類,讓這種類型的對象使用起來更簡單。你也可以使用數組和對象來實現本章展示的方法。但是定義一個Dictionary類更方便,也更有意思。比如,使用()就比使用[]簡單。當然,還有其它的一些便利,比如可以定義對整體進行操作的方法,舉個例子:顯示字典中所有的元素,這樣就不必在主程序中使用循環去遍歷整個字典了

一,Dictionary類

Dictionary類的基礎是Array類,而不是Object類。本章稍后將提到,我們想對字典中的鍵排序,而javascript中是不能對對象的屬性進行排序的。但要記住,javascript一切皆為對象,數組也是對象。

以下面的代碼開始定義Dictionary類:

    function Dictionary() {
        this.datastore = new Array();
    }


先來定義add()方法,該方法接受兩個參數,鍵和值。鍵是值在字典中的索引。代碼如下:

    function add(key, value) {
        this.datastore[key] = value;
    }

接下來定義find()方法,該方法以鍵為參數,返回和其關聯的值。代碼如下所示:

    function find(key) {
        return this.datastore[key]
    }


從字典中刪除鍵-值。需要使用javascript中一個內置函數:delete。該函數是Object類的一部分,使用對鍵的引用作為參數。該函數同時刪掉鍵和與其無關的值。下面是remove()的定義。

    function remove(key) {
        delete this.datastore[key] //delete是Object類的一部分,使用對鍵刪掉鍵和與其無關的值。
    }

最后,我們希望顯示字典中所有的鍵-值對,下面就是一個顯示的方法。

    function showAll() {
        for (var key in Object.keys(this.datastore)) {
            console.log(key + " -> " + this.datastore[key])
        }
    }

最后調用Object的keys()方法可以返回傳入參數中儲存的所有鍵。

測試方法

    function Dictionary() {
        this.add = add;
        this.datastore = new Array();
        this.find = find;
        this.remove  = remove;
        this.showAll = showAll;
    }

    //add()
    function add(key, value) {
        this.datastore[key] = value;
    }

    function find(key) {
        return this.datastore[key]
    }

    function remove(key) {
        delete this.datastore[key] //delete是Object類的一部分,使用對鍵刪掉鍵和與其無關的值。
    }

    function showAll() {
        for (var key in Object.keys(this.datastore)) {  //調用Object的keys()方法可以返回傳入參數中儲存的所有鍵
            console.log(key + " -> " + this.datastore[key])
        }
    }

    //測試
    var pbook = new Dictionary();
    pbook.add("Mike","123");
    pbook.add("David","345");
    pbook.add("Cynthia","456");
    console.log(" David's extension: " + pbook.find("David")) ;// David's extension: 345
    pbook.remove("David");
    pbook.showAll()

    mike - > 123
    Cynthia - > 456

二,Dictionary類的輔助方法

我們還可以定義一些在特定情況下有用的輔助方法,比如,統計字典中元素的個數

    function count() {
        var n = 0;
        for (var key in Object.keys(this.datastore)) {
            ++n;
        }
    }

此時,你可能問,為什么使用length屬性,這是因為當鍵的類型為為字符串時,length屬性就不管用了。測試:

    var nums = new Array();
    nums[0] = 1;
    nums[1] = 2;
    console.log(nums.length) //2

    pbook.add("Mike","123");
    pbook.add("David","345");

    console.log(pbook.length);//undefined

clear()也是另外一種輔助方法,定義如下:

    function clear() {
        for each (var key in Object.keys(this.datastore)) {
            delete this.datastore[key];
        }
    }

三,為Dictionary類添加排序功能

字典的用途主要是通過鍵取值,我們無需太關心數據在字典中的實際存儲順序。然而,很多人希望看到一個有序的字典。下面看看如何實現字典的按順序顯示。

數組是可以排序的。

如下。

    var a = new Array();
    a[0] = "Mike";
    a[1] = "David";
    console.log(a);//["Mike", "David"]
    a.sort();
    console.log(a) //["David", "Mike"]

但是上面的這種做法在以字符串作為鍵的字典是無效的,程序不會有任何輸出。這和我們定義count()方法時所遇到的情況是一樣的。

不過,這也不是大問題,用戶關心的是顯示字典的內容時,結果是有序的。可以使用Object.keys()函數解決這個問題。下面是重新定義showAll()的方法。

    function showAll2() {
        for (var key in Object.keys(this.datastore).sort()) {
            console.log(key + " -> " + this.datastore[key])
        }
    }

該定義和之前定義的唯一區別是:從數組datastore拿到鍵后,調用sort()方法對鍵重新排序。

    function Dictionary() {
        this.add = add;
        this.datastore = new Array();
        this.find = find;
        this.remove  = remove;
        this.showAll = showAll;
        this.showAll2 = showAll2;
        this.count = count;
        //this.clear = clear;
    }

    //add()
    function add(key, value) {
        this.datastore[key] = value;
    }

    function find(key) {
        return this.datastore[key]
    }

    function remove(key) {
        delete this.datastore[key] //delete是Object類的一部分,使用對鍵刪掉鍵和與其無關的值。
    }

    function showAll() {
        for (var key in Object.keys(this.datastore)) {  //調用Object的keys()方法可以返回傳入參數中儲存的所有鍵
            console.log(key + " -> " + this.datastore[key])
        }
    }

    function showAll2() {
        for (var key in Object.keys(this.datastore).sort()) { //顯示字典的內容時,結果是有序的。使用Object.keys()函數。
            console.log(key + " -> " + this.datastore[key])
        }
    }

    function count() {
        var n = 0;
        for (var key in Object.keys(this.datastore)) {
            ++n;
        }
        console.log(n)
    }

    // function clear() {
    //     for each (var key in Object.keys(this.datastore)) {
    //         delete this.datastore[key];
    //     }
    // }


    //測試
    var pbook = new Dictionary();
    pbook.add("Mike","123");
    pbook.add("David","345");
    pbook.add("Cynthia","456");
    console.log(" David's extension: " + pbook.find("David")) ;// David's extension: 345
    pbook.remove("David");
    pbook.showAll()
    pbook.count()//2
    //pbook.clear()
    pbook.showAll2()

 

本章完結

上一章:第五章:javascript:隊列   下一章:第七章:javascript:散列


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM