jQuery HashTable

本文介绍了一个使用jQuery实现的简单哈希表数据结构。该哈希表支持添加、获取、删除等基本操作,并提供了检查键或值是否存在等功能。通过示例展示了如何在网页中利用这个自定义哈希表进行数据管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jQuery.Hashtable = function() {
02     this.items = new Array();
03     this.itemsCount = 0;
04     this.add = function(key, value) {
05         if (!this.containsKey(key)) {
06             this.items[key] = value;
07             this.itemsCount++;
08         }
09         else
10             throw "key '" + key + "' allready exists."
11     }
12     this.get = function(key) {
13         if (this.containsKey(key))
14             return this.items[key];
15         else
16             return null;
17     }
18   
19     this.remove = function(key) {
20         if (this.containsKey(key)) {
21             delete this.items[key];
22             this.itemsCount--;
23         }
24         else
25             throw "key '" + key + "' does not exists."
26     }
27     this.containsKey = function(key) {
28         return typeof (this.items[key]) != "undefined";
29     }
30     this.containsValue = function containsValue(value) {
31         for (var item in this.items) {
32             if (this.items[item] == value)
33                 return true;
34         }
35         return false;
36     }
37     this.contains = function(keyOrValue) {
38         return this.containsKey(keyOrValue) || this.containsValue(keyOrValue);
39     }
40     this.clear = function() {
41         this.items = new Array();
42         itemsCount = 0;
43     }
44     this.size = function() {
45         return this.itemsCount;
46     }
47     this.isEmpty = function() {
48         return this.size() == 0;
49     }
50};
 

=======================================================

 

var hashtable = new jQuery.Hashtable();
2 $(function() {
3     $('#btnAdd').click(function() {
4         hashtable.add($('#txtAddKey').val(), $('#txtAddValue').val());
5     });
6     $('#btnGet').click(function() {
7         alert(hashtable.get($('#txtGetKey').val()))
8     });
9})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值