zoukankan      html  css  js  c++  java
  • 前端Map封装源码

    源于后台思路,简单封装了一下Map插件,方便以后使用。

      1 function Map() {
      2     this.elements = new Array();
      3 
      4     //获取MAP元素个数
      5     this.size = function() {
      6         return this.elements.length;
      7     };
      8 
      9     //判断MAP是否为空
     10     this.isEmpty = function() {
     11         return (this.elements.length < 1);
     12     };
     13 
     14     //删除MAP所有元素
     15     this.clear = function() {
     16         this.elements = new Array();
     17     };
     18 
     19     //向MAP中增加元素(key, value)
     20     this.put = function(_key, _value) {
     21         this.elements.push({
     22             key: _key,
     23             value: _value
     24         });
     25     };
     26 
     27     //删除指定KEY的元素,成功返回True,失败返回False
     28     this.remove = function(_key) {
     29         try {
     30             for (i = 0; i < this.elements.length; i++) {
     31                 if (this.elements[i].key == _key) {
     32                     this.elements.splice(i, 1);
     33                     return true;
     34                 }
     35             }
     36         } catch (e) {
     37             return false;
     38         }
     39     };
     40 
     41     //获取指定KEY的元素值VALUE,失败返回NULL
     42     this.get = function(_key) {
     43         try {
     44             for (i = 0; i < this.elements.length; i++) {
     45                 if (this.elements[i].key == _key) {
     46                     return this.elements[i].value;
     47                 }
     48             }
     49         } catch (e) {
     50             return null;
     51         }
     52     };
     53 
     54     //获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL
     55     this.element = function(_index) {
     56         if (_index < 0 || _index >= this.elements.length) {
     57             return null;
     58         }
     59         return this.elements[_index];
     60     };
     61 
     62     //判断MAP中是否含有指定KEY的元素
     63     this.containsKey = function(_key) {
     64         try {
     65             for (i = 0; i < this.elements.length; i++) {
     66                 if (this.elements[i].key == _key) {
     67                     return true;
     68                 }
     69             }
     70         }
     71         catch (e) {
     72             return false;
     73         }
     74     };
     75 
     76     //判断MAP中是否含有指定VALUE的元素
     77     this.containsValue = function(_value) {
     78         try {
     79             for (i = 0; i < this.elements.length; i++) {
     80                 if (this.elements[i].value == _value) {
     81                     return true;
     82                 }
     83             }
     84         }
     85         catch (e) {
     86             return false;
     87         }
     88     };
     89 
     90     //获取MAP中所有VALUE的数组(ARRAY)
     91     this.values = function() {
     92         var arr = new Array();
     93         for (i = 0; i < this.elements.length; i++) {
     94             arr.push(this.elements[i].value);
     95         }
     96         return arr;
     97     }
     98 
     99     //获取MAP中所有KEY的数组(ARRAY)
    100     this.keys = function() {
    101         var arr = new Array();
    102         for (i = 0; i < this.elements.length; i++) {
    103             arr.push(this.elements[i].key);
    104         }
    105         return arr;
    106     };
    107 }
  • 相关阅读:
    写爬虫,怎么可以不会正则呢?
    从 Scrapy 学习模块导入技巧
    博客已搬家至CSDN
    更改JDK默认编码,解决DBeaver乱码问题
    【2020面试】- Java中常见集合的默认大小以及扩容机制
    【2020面试】- CAS机制与自旋锁
    【2020面试】- filter和interceptor的区别
    【20k中级开发】-面试题201117
    【开发笔记】
    RPC 调用和 HTTP 调用的区别
  • 原文地址:https://www.cnblogs.com/guomin/p/7305191.html
Copyright © 2011-2022 走看看