JavaScript处理数组的函数方法

1.push往末尾添加一个元素

Array.prototype.myPush = function () {
    for (var i = 0; i < arguments.length; i++) {
        this[this.length] = arguments[i];
    }
    return this.length;
}

2.pop删除末尾的一个元素

Array.prototype.myPop = function () {
    var temp = this[this.length - 1];
    delete this[this.length - 1];
    this.length -= 1;
    return temp;
}

3.unshift往数组前添加一个或多个元素

Array.prototype.myUnshift = function () {
    if (arguments.length) {
        for (var i = arguments.length - 1; i >= 0; i--) {
            for (var j = this.length - 1; j >= 0; j--) {
                this[j + 1] = this[j];
            }
            this[0] = arguments[i];
        }
        return this;
    } else {
        return this;
    }
}

4.shift删除第一个元素

Array.prototype.myShift = function () {
    var temp = this[0];
    for (var i = 0; i < this.length; i++) {
        this[i] = this[i + 1];
    }
    delete this[this.length - 1];
    this.length -= 1;
    return temp;
}

5.reverse数组逆转

Array.prototype.myReverse = function () {
    var tempArray = [];
    for (var i = this.length - 1; i >= 0; i--) {
        tempArray.myPush(this[i]); //使用myPush
    }
    for (var j = 0; j < this.length; j++) {
        this[j] = tempArray[j];
    }
    return this;
}

6.splice(n,m,x…) 第n位开始,截取m长度,并在切口里添加一个或多个元素

Array.prototype.mySplice = function (n, m) {
    if (arguments.length == 0) {
        return this;
    } else if (arguments.length == 1) {
        return this[n];
    } else if (arguments.length == 2) {
        if (n + m <= this.length - 1) {
            for (var j = n + m; j < this.length; j++) {
                this[j - m] = this[j]
            }
            this.length -= m;
            return this;
        } else {
            return this;
        }
    } else {
        if (n + m <= this.length - 1) {
            for (var j = n + m; j < this.length; j++) {
                this[j - m] = this[j]
            }
            this.length -= m;
            for (var i = arguments.length - 1; i >= 2; i--) {
                for (var k = this.length - 1; k >= n; k--) {
                    this[k + 1] = this[k]
                }
                this[n] = arguments[i];
            }
            return this;
        } else {
            return this;
        }
    }
}

7.sort排序,自己封装的排序方法有很多,这里列举两种,全是从小到大排
(1)冒泡排序

Array.prototype.mySort_bubble = function () {
    for (var i = this.length; i >= 0; i--) {
        for (var j = 0; j < i; j++) {
            if (this[j] > this[j + 1]) {
                var temp = this[j];
                this[j] = this[j + 1];
                this[j + 1] = temp;
            }
        }
    }
    return this;
}

(2)选择排序

Array.prototype.mySort_choose = function () {
    for (var i = 0; i < this.length; i++) {
        var min = i;
        for (var j = i + 1; j < this.length; j++) {
            if (this[j] < this[min]) {
                min = j;
            }
        }
        var temp = this[min];
        this[i] = this[min];
        this[min] = temp;
    }
}

8.concat数组拼接

Array.prototype.myConcat = function () {
    var tempArray = this;
    for (var i = 0; i < arguments[0].length; i++) {
        tempArray.myPush(arguments[0][i]);
    }
    return tempArray;
}

9.toString 转化成一个字符

Array.prototype.myToString = function () {
    var temp = "[";
    for (var i = 0; i < this.length; i++) {
        if (i != this.length - 1) {
            temp = temp + this[i] + ",";
        } else {
            temp = temp + this[i] + "]";
        }
    }
    return temp;
}

10.slice截取

/**
 * (1)没有参数,截取整个数组
 * (2)一个参数,从第n位截取到最后1位
 * (3)两个参数,从第n位截取到第m位
 */
Array.prototype.mySlice = function () {
    var temp = [];
    if (arguments.length == 0) {
        return this;
    } else if (arguments.length == 1) {
        for (var i = arguments[0]; i < this.length; i++) {
            temp.myPush(this[i]);
        }
    } else {
        if (arguments[0] < arguments[1]) {
            for (var j = arguments[0]; j < arguments[1] + 1; j++) {
                temp.myPush(this[j]);
            }
        } else {
            return this;
        }
    }
    return temp;
}

11.数组去重

Array.prototype.unique = function () {
    var temp = {};
    var array = [];
    for(var i = 0;i<this.length;i++){
        if(!temp[this[i]]){
            temp[this[i]]=true;
            array.myPush(this[i]);
        }
    }
    return array;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值