javascript Dictionary data structures

Dictionary常被称为数据字典,是一种用来保存键值对的数据结构,比如我们日常的电话本,就是一个Dictionary。我们通过键(名字),就可以访问到对应的值(电话号码)。在C++与java中我们都是使用map来构建这样一个Dictionary,只是名字不同而已。在javascript中对象就好像是一个Dictionary一样,因为对象就是一个属性名/属性值的集合。

为了更好的体现出Dictionary,我们可以基于Array与object来构建一个使用方便简单的Dictionary类:

Dictionary类:

<html>
<head>
    <title>Date Example</title>
</head>
<body>
<div id="content"></div>
<input type="button" value="Set InnerHTML" onclick="testDate()">

<script type="text/javascript">

  function Dictionary (){
      this.dataArray = [];

    //   添加元素
      this.add = function (key, item){
        this.dataArray[key] = item;
      };

    //   查找元素
      this.find = function (key){
        return this.dataArray[key];
      };

    //   删除元素
      this.remove = function (key){
        delete this.dataArray[key];
      };

    //   显示元素
      this.showAllItems = function (){
          for (var key in Object.keys(this.dataArray)){
            console.log(key + ":" + this.dataArray[key]);
          }
      }

  }

</script>
</body>
</html>

除了上面这些方法之外,我们也可以根据自己的使用来添加其他的方法与属性。

//   删除字典中所有元素this.clearAll = function (){
          for (var key in Object.keys(this.dataArray)){
            delete this.dataArray[key];
          }
      };
//   字典中的元素个数this.itemsCount = function (){
          var num = 0;
          for (var key in Object.keys(this.dataArray)){
            ++num;
          }
          return num;
      }

在itemsCount中查询数量为什么不直接使用this.dataArray.length呢???这是因为在键的类型为字符串类型的时候,length这个属性就不起什么作用了。。。。

字典的用途大多说时候我们之关心通过键开获取值,不会去关心键的顺序问题,但是为了更好的显示我们前面说的那个电话本,可以对电话本中的键(名字)进行排序。

在javascript中我们常见的是对数组进行排序,那利用数组的sort()进行排序如何呢,你会看到silently。主要原因就是我们平时使用的数组是在数组按照整型值的索引下对数组元素进行排序的,现在的数组的索引变成了字符串,这种形式的排序也就失去了效果。其实我们可以取出Dictionary中的键,把键放入一个数组A中,然后对这个数组A进行排序,然后利用数组A来遍历Dictionary。Object.keys()就提供了这样一种方法:

this.sequenceShow = function (){
          for (var key in Object.keys(this.dataArray).sort()){
            console.log(key + ":" + this.dataArray[key]);
          }
      }

这样我们就可以实现把“键”进行排序了。。。

时间: 2024-10-19 07:38:28

javascript Dictionary data structures的相关文章

javascript Set data structures

集合(set)是一组无序的,但彼此之间又有一定相关性的数据集.每个成员在数组中只能出现一次. 在使用集合(set)之前最好先理解一下内容: 1.不包含任何成员的集合称为空集合. 2.如果两个集合的成员都相等,两个集合相等. 3.如果一个集合中的成员都在另一个集合中,则两个具有父子集的关系. 在集合中我们常用的操作就是:求集合的并集,交集,补集等. 下面我们基于数组该构建一个集合(Set)类. <html> <head> <title>Date Example</t

Data Structures and Algorithms with JavaScript

Book Description As an experienced JavaScript developer moving to server-side programming, you need to implement classic data structures and algorithms associated with conventional object-oriented languages like C# and Java. This practical guide show

JavaScript data types and data structures

JavaScript data types and data structures Programming languages all have built-in data structures, but these often differ from one language to another. This article attempts to list the built-in data structures available in JavaScript and what proper

[Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures

To demonstrate the difference between mutability and immutability, imagine taking a drink from a glass of water. If our glass is mutable, when we take a drink, we retain the same glass and change the amount of water in that glass. However, if our gla

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

Go Data Structures: Interfaces

原文链接 http://research.swtch.com/interfaces Go's interfaces—static, checked at compile time, dynamic when asked for—are, for me, the most exciting part of Go from a language design point of view. If I could export one feature of Go into other languages

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&

【DataStructure】Linked Data Structures

Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure. so inserting an elemen