js 实现字典

js 有两种数据结构,array和object,es6又加了两种map和set。js实现字典看似要用object,其实应该使用数组Array,因为数组也是object

代码实现:

/*=======字典========*/
var Dictionary=function(){
    this.data=[]
    this.find=find
    this.add=add
    this.remove=remove
}
//查找
var find=function(key){
    return this.data[key]
}
//添加
var add=function(key,value){
    this.data[key]=value
}
//移除
var remove=function(key){
    delete this.data[key]
}



/*======测试========*/
var d=new Dictionary()
d.add('name','张三')
d.add('age','12')
d.add('sex','男')
console.log(d)
console.log(d.find("age"))
d.remove('age')
console.log(d)

 

posted @ 2018-11-05 21:09  mingL  阅读(3672)  评论(0编辑  收藏  举报