技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> javascript扩展Array(数组)类

javascript扩展Array(数组)类

浏览:3095次  出处信息

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);


建议继续学习:

  1. C语言结构体里的成员数组和指针    (阅读:4591)
  2. 为什么数组标号是从0开始的?    (阅读:4594)
  3. 将数组定义为常量    (阅读:4440)
  4. Tips of Linux C programming    (阅读:3755)
  5. xml转数组的方法    (阅读:3272)
  6. 动态数组的 C 实现    (阅读:2964)
  7. php数组排序    (阅读:2872)
  8. javascript数组排序的问题    (阅读:2847)
  9. 关于Cannot use a scalar value as an array的解决办法    (阅读:2769)
  10. “C++的数组不支持多态”?    (阅读:2674)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1