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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM