好鋼得用在刀刃上……
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
隨筆-9 評論-0 文章-0 trackbacks-0
JavaScript 實(shí)現(xiàn)類似hashtable 的功能
頁面上經(jīng)常需要臨時(shí)保存一些數(shù)據(jù),這些數(shù)據(jù)需要根據(jù)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];}
//
/記錄條數(shù)
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
++
;
}
}
//
/返回值、根據(jù)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、根據(jù)Key返回Value
hashTab.items(strKey)
7、返回記錄條數(shù)
hashTab.count()
8、根據(jù)IndexId返回第幾條數(shù)據(jù)(遍歷的時(shí)用)
hashTab.indexValue(IndexId)
9、清空所有數(shù)據(jù)
hashTab.clear()
posted on 2009-10-30 12:34
AndyFish
閱讀(332)
評論(0)
編輯
收藏
所屬分類:
JavaScript
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關(guān)文章:
JavaScript 實(shí)現(xiàn)類似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)
基礎(chǔ)知識(3)
隨筆檔案
2010年3月 (1)
2010年2月 (4)
2010年1月 (1)
2009年11月 (1)
2009年10月 (2)
搜索
最新評論
閱讀排行榜
1.?JavaScript 實(shí)現(xiàn)類似hashtable 的功能(332)
2.?Struts2攔截器的使用 (詳解) (277)
3.?hibenate的面試總結(jié)(195)
4.?String與StringBuffer的區(qū)別(155)
5.?本田宗一郎經(jīng)營語錄(141)
評論排行榜
1.?hibenate的面試總結(jié)(0)
2.?Struts2攔截器的使用 (詳解) (0)
3.?職場杰出人士的20個(gè)好習(xí)慣(0)
4.?公司絕不會告訴你的10大秘密(0)
5.?本田宗一郎經(jīng)營語錄(0)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 AndyFish
主站蜘蛛池模板:
曲沃县
|
镇坪县
|
乐至县
|
碌曲县
|
昂仁县
|
绥中县
|
民权县
|
全州县
|
航空
|
淮北市
|
德格县
|
扎兰屯市
|
蓝山县
|
沂水县
|
肥城市
|
海盐县
|
乌兰察布市
|
广汉市
|
枣阳市
|
天镇县
|
阿图什市
|
华池县
|
广东省
|
崇文区
|
安陆市
|
武清区
|
栾川县
|
青龙
|
九江县
|
札达县
|
乌兰浩特市
|
台江县
|
黔南
|
蛟河市
|
汤阴县
|
祁东县
|
黄平县
|
嘉黎县
|
个旧市
|
永胜县
|
韶山市
|