javascript扩展Array类

来源:互联网 发布:linux同步文件夹 编辑:程序博客网 时间:2024/05/20 06:41

1、用于清空数组

Array.prototype.clear = function() {    this.length = 0;}

2、判断数据项在数组中的位置

varoldArrayIndexOf = Array.indexOf;//判断是否原始浏览器是否存在indexOf方法Array.prototype.indexOf = function(obj) {    if(!oldArrayIndexOf) {        for(vari = 0, imax = this.length; i < imax; i++) {            if(this[i] === obj) {                returni;            }        }        return-1;    } else{        returnoldArrayIndexOf(obj);    }}

3、判断数据项是否在该数组中

Array.prototype.contain = function(obj) {    returnthis.indexOf(obj) !== -1;}

4、把数据项添加到指定的位置

Array.prototype.insertAt = function(index, obj) {    if(index < 0) index = 0;     if(index > this.length) index = this.length;    this.length++;    for(vari = this.length - 1; i > index; i--) {        this[i] = this[i - 1];    }    this[index] = obj;}

5、返回最有一项数据

Array.prototype.last = function() {    returnthis[this.length - 1];}

6、移除数组指定索引的值

Array.prototype.removeAt = function(index) {    if(index < 0 || index >= this.length) return;    varitem = this[index];    for(vari = index, imax = this.length - 2; i < imax; i++) {         this[i] = this[i + 1];     }     this.length--;     returnitem; }

7、移除数据项的数据

Array.prototype.removeAt = function(obj) {     varindex = this.indexOf(obj);     if(index >= 0)        this.removeAt(index);}

8、用于数组的查询
用于查询对象数组中对象的某些值,同时支持对已查询属性进行重命名,若查询的属性不在改数组中,则该属性返回为undefined

Array.prototype.select = function(args) {    varnewItems = [];    if(typeof(args) === "object"&& arguments.length === 1) {//传入查询的参数为对象时的处理方式        for(vari = 0, imax = this.length; i < imax; i++) {            varitem = {};            for(varkey inargs) {                if(args[key] !== undefined) {                    item[key] = this[i][key] === undefined ? "undefined": this[i][key];                }            }            newItems.push(item);        }    } elseif(typeof(args) === "string"&& arguments.length === 1) {//传入参数为字符串,且只有一个参数的处理方式        for(vari = 0, imax = this.length; i < imax; i++) {            varitem = {};            varkeys = args.split(',');            for(vark = 0, kmax = keys.length; k < kmax; k++) {                variKey = keys[k].split("as");                if(iKey.length === 1) {                    item[iKey[0].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()];                } else{                    item[iKey[1].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()];                }            }            newItems.push(item);        }    } else{//传入的参数是多个字符串的处理方式        for(vari = 0, imax = this.length; i < imax; i++) {            varitem = {};            for(varj = 0, jmax = arguments.length; j < jmax; j++) {                if(arguments[j] !== undefined) {                    variKey = arguments[j].split("as");                    if(iKey.length === 1) {                        item[iKey[0].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()];                    } else{                        item[iKey[1].trim()] = this[i][iKey[0].trim()] === undefined ? "undefined": this[i][iKey[0].trim()];                    }                }            }            newItems.push(item);        }    }    returnnewItems;}

假设数据对象数组为:var obj = [{ name: "张三", second.age: "24", sex: "男" }
, { name: "张二", age: "21", sex: "女" }
, { name: "张一", age: "23", sex: "男" }
, { name: "张四", age: "25", sex: "女" }
, { name: "张五", age: "22", sex: "男"}];

例一:obj.select(“name,second.age as age,sex”);
例二:obj.select(“name”,”second.age as age”,”sex”);
例三:查询属性对象var arg={name:”",second.age:”",sex:”"};
obj.select(arg);


原创粉丝点击