首页 > 代码库 > 扩展自己的数组核心库

扩展自己的数组核心库

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <script src=http://www.mamicode.com/"http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>    <!--引入东师理想JS框架基础核心库-->    <script src=http://www.mamicode.com/"dsFrameWork_Core.js"></script>    </head><body></body><script>$(function(){    var ds = [    {"chat_id":"1111","chat_name":"1111","chat_type":"2"},    {"chat_id":"2222","chat_name":"2222","chat_type":"2"},    {"chat_id":"3333","chat_name":"3333","chat_type":"3"},    {"chat_id":"4444","chat_name":"4444","chat_type":"3"},    {"chat_id":"5555","chat_name":"5555","chat_type":"3"}];    var map1 = new ds_Array(ds,"chat_id");        //返回指定关键词相应的对象    var o=map1.getObject("5555");    //map1.changeTop("400329");    //console.log(obj2Str(ds));    //map1.changeBottom("400329");    console.log(obj2Str(ds));    map1.changePostion("2222","1111","before");    console.log(obj2Str(ds));    map1.changePostion("2222","5555","after");    console.log(obj2Str(ds));    });</script></html>

dsFrameWork_Core.js

/*    * 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)    *    * 例子:    * var map = new Map();    *    * map.put("key", "value");    * var val = 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.remove = 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;         }                //获取指定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 null;             }         }                //获取指定索引的元素(使用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) {             varbln = 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 =http://www.mamicode.com/ function(_value) {             var bln = false;             try{                 for(i = 0; i < this.elements.length; i++) {                     if(this.elements[i].value =http://www.mamicode.com/= _value) {                         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中所有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;         }     }   function obj2Str(obj){  switch(typeof(obj)){     case object:      var ret = [];      if (obj instanceof Array){       for (var i = 0, len = obj.length; i < len; i++){        ret.push(obj2Str(obj[i]));       }       return [ + ret.join(,) + ];      }      else if (obj instanceof RegExp){       return obj.toString();      }      else{       for (var a in obj){        ret.push(a + : + obj2Str(obj[a]));       }       return { + ret.join(,) + };      }     case function:      return function() {};     case number:      return obj.toString();     case string:      return "\"" + obj.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function(a) {return ("\n"==a)?"\\n":("\r"==a)?"\\r":("\t"==a)?"\\t":"";}) + "\"";     case boolean:      return obj.toString();     default:      return obj.toString();  }  }  /*************************************************************************************************************//*功能:扩展的东师理想自己的Map类型,传入一个JSON数据,和一个KEY字段,      在内存中生成两个Map对象,一个可以通过Key返回索引号,另一个可以通过Key返回实体对象作者:黄海时间:2014-07-31用例:var ds = [    {"chat_id":"1111","chat_name":"1111","chat_type":"2"},    {"chat_id":"2222","chat_name":"2222","chat_type":"2"},    {"chat_id":"3333","chat_name":"3333","chat_type":"3"},    {"chat_id":"4444","chat_name":"4444","chat_type":"3"},    {"chat_id":"5555","chat_name":"5555","chat_type":"3"}];    var map1 = new ds_Array(ds,"chat_id");        //返回指定关键词相应的对象    var o=map1.getObject("5555");    map1.changeTop("400329");    map1.changeBottom("400329");    map1.changePostion("2222","1111","before");    map1.changePostion("2222","1111","after");*/function ds_Array(json,key_field){       var Key_map,Object_map;        var init=function()    {        Key_map = new Map();            Object_map = new Map();                    $.each(json,function(i,n)        {            var o=eval(n.+key_field);            Key_map.put(o,i);            Object_map.put(o,n);        });     }    init();            this.getObject = function(_key)    {        return Object_map.get(_key);    }            this.changePostion  = function(_key1,_key2,_dir)    {        var myIndex1,myObject1,myIndex2;        switch(_dir)        {            case "after"://_dir:1 _key1向_key2 后                myIndex1=Key_map.get(_key1);                                myObject1=Object_map.get(_key1);                myIndex2=Key_map.get(_key2);                if(myIndex1>myIndex2)                {                    json.splice(myIndex1,1);                    json.splice(myIndex2,0,myObject1);                  }                else                {                    json.splice(myIndex2+1,0,myObject1);                    json.splice(myIndex1,1);                }                break;                    case "before"://_dir:2 _key1向_key2 前                myIndex1=Key_map.get(_key1);                myObject1=Object_map.get(_key1);                myIndex2=Key_map.get(_key2);                if(myIndex2>myIndex1)                {                    json.splice(myIndex2,0,myObject1);                    json.splice(myIndex1,1);                 }                else                {                    json.splice(myIndex1,1);                     json.splice(myIndex2,0,myObject1);                }                break;        }        init();    }    this.changeTop  =  function(_key)    {        //将_key1放到顶部        var myIndex=Key_map.get(_key);        var myObject=Object_map.get(_key);        //删除指定索引的对象        json.splice(myIndex,1);        //放到第一个上        json.unshift(myObject);        init();    }    this.changeBottom=function(_key)    {        //将_key1放到底部        var myIndex=Key_map.get(_key);        var myObject=Object_map.get(_key);        //删除指定索引的对象        json.splice(myIndex,1);        //放到最后一个上        json.push(myObject);        init();    }    }