第六章: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:散列

相关文章
相关标签/搜索