首页 文章 精选 留言 我的

精选列表

搜索[javascript库],共10000篇文章
优秀的个人博客,低调大师

JavaScript之map的使用

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 /* *MAP对象,实现MAP功能 * *接口: *size()获取MAP元素个数 *isEmpty()判断MAP是否为空 *clear()删除MAP所有元素 *put(key,value)向MAP中增加元素(key,value) *remove(key)删除指定KEY的元素,成功返回True,失败返回False *get(key)获取指定KEY的元素值VALUE,失败返回NULL *element(index)获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL *containsKey(key)判断MAP中是否含有指定KEY的元素 *containsValue(value)判断MAP中是否含有指定VALUE的元素 *values()获取MAP中所有VALUE的数组(ARRAY) *keys()获取MAP中所有KEY的数组(ARRAY) * *例子: *varmap=newMap(); * *map.put("key","value"); *varval=map.get("key") *…… * */ function Map(){ this .elements= new Array(); //获取MAP元素个数 this .size= function (){ return this .elements.length; }; //判断MAP是否为空 this .isEmpty= function (){ return ( this .elements.length<1); }; //删除MAP所有元素 this .clear= function (){ this .elements= new Array(); }; //向MAP中增加元素(key,value) this .put= function (_key,_value){ this .elements.push({ key:_key, value:_value }); }; //删除指定KEY的元素,成功返回True,失败返回False this .removeByKey= function (_key){ var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].key==_key){ this .elements.splice(i,1); return true ; } } } catch (e){ bln= false ; } return bln; }; //删除指定VALUE的元素,成功返回True,失败返回False this .removeByValue= function (_value){ //removeByValueAndKey var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].value==_value){ this .elements.splice(i,1); return true ; } } } catch (e){ bln= false ; } return bln; }; //删除指定VALUE的元素,成功返回True,失败返回False this .removeByValueAndKey= function (_key,_value){ var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].value==_value&& this .elements[i].key==_key){ this .elements.splice(i,1); return true ; } } } catch (e){ bln= false ; } return bln; }; //获取指定KEY的元素值VALUE,失败返回NULL this .get= function (_key){ try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].key==_key){ return this .elements[i].value; } } } catch (e){ return false ; } return false ; }; //获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL this .element= function (_index){ if (_index<0||_index>= this .elements.length){ return null ; } return this .elements[_index]; }; //判断MAP中是否含有指定KEY的元素 this .containsKey= function (_key){ var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].key==_key){ bln= true ; } } } catch (e){ bln= false ; } return bln; }; //判断MAP中是否含有指定VALUE的元素 this .containsValue= function (_value){ var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].value==_value){ bln= true ; } } } catch (e){ bln= false ; } return bln; }; //判断MAP中是否含有指定VALUE的元素 this .containsObj= function (_key,_value){ var bln= false ; try { for (i=0;i< this .elements.length;i++){ if ( this .elements[i].value==_value&& this .elements[i].key==_key){ bln= true ; } } } catch (e){ bln= false ; } return bln; }; //获取MAP中所有VALUE的数组(ARRAY) this .values= function (){ var arr= new Array(); for (i=0;i< this .elements.length;i++){ arr.push( this .elements[i].value); } return arr; }; //获取MAP中所有VALUE的数组(ARRAY) this .valuesByKey= function (_key){ var arr= new Array(); for (i=0;i< this .elements.length;i++){ if ( this .elements[i].key==_key){ arr.push( this .elements[i].value); } } return arr; }; //获取MAP中所有KEY的数组(ARRAY) this .keys= function (){ var arr= new Array(); for (i=0;i< this .elements.length;i++){ arr.push( this .elements[i].key); } return arr; }; //获取key通过value this .keysByValue= function (_value){ var arr= new Array(); for (i=0;i< this .elements.length;i++){ if (_value== this .elements[i].value){ arr.push( this .elements[i].key); } } return arr; }; //获取MAP中所有KEY的数组(ARRAY) this .keysRemoveDuplicate= function (){ var arr= new Array(); for (i=0;i< this .elements.length;i++){ var flag= true ; for ( var j=0;j<arr.length;j++){ if (arr[j]== this .elements[i].key){ flag= false ; break ; } } if (flag){ arr.push( this .elements[i].key); } } return arr; }; } 转载链接:http://www.cnblogs.com/fengys-moving/archive/2012/08/26/2657417.html

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册