第六章:javascript:字典

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

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

一,Dictionary類java

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

如下面的代碼開始定義Dictionary類:數據結構

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


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

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

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

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


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

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

最後,咱們但願顯示字典中全部的鍵-值對,下面就是一個顯示的方法。this

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

最後調用Object的keys()方法能夠返回傳入參數中儲存的全部鍵。spa

測試方法

    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:散列

相關文章
相關標籤/搜索