JavaScript Array 對象擴充方法

來源:互聯網
上載者:User
/** 刪除數組中指定索引的資料 **/Array.prototype.deleteAt = function (index) {    if (index < 0) {        return this;    }    return this.slice(0, index).concat(this.slice(index + 1, this.length));}/** 數組洗牌 **/Array.prototype.random = function () {    var tempArr = [], me = this, t;    while (me.length > 0) {        t = Math.floor(Math.random() * me.length);        tempArr[tempArr.length] = me[t];        me = me.deleteAt(t);    }    return tempArr;}Array.prototype.orderRandom = function () {    return this.sort(function () {        return Math.random() > 0.5 ? "-1" : "1";    });}/** 數字數組排序 **/Array.prototype.sortNum = function (i) {    if (!i) {        i = 0;    }    if (i == 1) {        return this.sort(function (a, b) {            return b - a;        });    }    return this.sort(function (a, b) {        return a - b;    });}/** 擷取數字數組中的最大項 **/Array.prototype.getMax = function () {    return this.sortNum(1)[0];}/** 擷取數字數組中的最小項 **/Array.prototype.getMin = function () {    return this.sortNum(0)[0];}/** 數組第一次出現指定元素的位置 **/Array.prototype.indexOf = function (o) {    for (var i = 0; i < this.length; i++) {        if (this[i] == o) {            return i;        }    }    return -1;}/** 去除數組中的重複項 **/Array.prototype.arrUnique = function () {    var reset = [], done = {};    for (var i = 0; i < this.length; i++) {        var temp = this[i];        if (!done[temp]) {            done[temp] = true;            reset.push(temp);        }    }    return reset;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.