好鋼得用在刀刃上……
BlogJava
首頁
新隨筆
聯系
聚合
管理
隨筆-9 評論-0 文章-0 trackbacks-0
JavaScript 實現類似hashtable 的功能
頁面上經常需要臨時保存一些數據,這些數據需要根據Key來保存或者修改Value,查看了些資料,稍微修改了一下。
如下:
function
HashTable()
{
this
._hash
=
new
Object();
this
.add
=
function
(key,value)
{
if
(
typeof
(key)
!=
"
undefined
"
)
{
if
(
this
.contains(key)
==
false
)
{
this
._hash[key]
=
typeof
(value)
==
"
undefined
"
?
null
:value;
return
true
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
this
.update
=
function
(key,value)
{
if
(
typeof
(key)
!=
"
undefined
"
)
{
if
(
this
.contains(key)
==
true
)
{
this
.remove(key);
this
.add(key,value);
return
true
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
//
/刪除
this
.remove
=
function
(key)
{
delete
this
._hash[key];}
//
/記錄條數
this
.count
=
function
()
{
var
i
=
0
;
for
(
var
k
in
this
._hash)
{
i
++
;
}
return
i;
}
this
.indexValue
=
function
(index)
{
var
i
=
0
;
for
(
var
k
in
this
._hash)
{
if
(i
==
index)
{
return
this
._hash[k];
}
i
++
;
}
}
//
/返回值、根據KEY值來返回
this
.items
=
function
(key)
{
return
this
._hash[key];}
//
/是否存在true or false;
this
.contains
=
function
(key)
{
return
typeof
(
this
._hash[key])
!=
"
undefined
"
;
}
//
/清空
this
.clear
=
function
()
{
for
(
var
k
in
this
._hash)
{
delete
this
._hash[k];
}
}
}
使用方法:
1、聲明
var
hashTab
=
new
HashTable();
2、添加Key和Value
hashTab.add(strKey,strValue)
3、修改
hashTab.update(strKey,strValue);
4、判斷Key是否存在
hashTab.contains(strKey);
5、刪除Key
hashTab.remove(strKey)
6、根據Key返回Value
hashTab.items(strKey)
7、返回記錄條數
hashTab.count()
8、根據IndexId返回第幾條數據(遍歷的時用)
hashTab.indexValue(IndexId)
9、清空所有數據
hashTab.clear()
posted on 2009-10-30 12:34
AndyFish
閱讀(334)
評論(0)
編輯
收藏
所屬分類:
JavaScript
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
JavaScript 實現類似hashtable 的功能
insertAdjacentElement Method
<
2009年10月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
JavaScript(2)
基礎知識(3)
隨筆檔案
2010年3月 (1)
2010年2月 (4)
2010年1月 (1)
2009年11月 (1)
2009年10月 (2)
搜索
最新評論
閱讀排行榜
1.?JavaScript 實現類似hashtable 的功能(334)
2.?Struts2攔截器的使用 (詳解) (278)
3.?hibenate的面試總結(196)
4.?String與StringBuffer的區別(157)
5.?本田宗一郎經營語錄(143)
評論排行榜
1.?hibenate的面試總結(0)
2.?Struts2攔截器的使用 (詳解) (0)
3.?職場杰出人士的20個好習慣(0)
4.?公司絕不會告訴你的10大秘密(0)
5.?本田宗一郎經營語錄(0)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 AndyFish
主站蜘蛛池模板:
宁海县
|
鄂托克旗
|
介休市
|
运城市
|
永和县
|
屏东市
|
东兰县
|
通道
|
鄂州市
|
高尔夫
|
平遥县
|
松江区
|
梁平县
|
昌吉市
|
鹰潭市
|
平和县
|
上饶县
|
宝兴县
|
琼结县
|
洪雅县
|
梧州市
|
金溪县
|
文登市
|
石城县
|
维西
|
沐川县
|
吴川市
|
塘沽区
|
乌什县
|
蓬安县
|
永吉县
|
玉溪市
|
社旗县
|
武乡县
|
山西省
|
陆良县
|
安塞县
|
通河县
|
永仁县
|
延庆县
|
库伦旗
|