Array對象擴展

来源:http://www.cnblogs.com/laixiangran/archive/2016/01/07/5110288.html
-Advertisement-
Play Games

/** * Created by laixiangran on 2016/01/07. * Array擴展 */(function() { // 遍曆數組 if (typeof Array.prototype.forEach != "function") { Array.p...


/**
 * Created by laixiangran on 2016/01/07.
 * Array擴展
 */
(function() {

    // 遍曆數組
    if (typeof Array.prototype.forEach != "function") {
        Array.prototype.forEach = function (fn, context) {
            for (var i = 0; i < this.length; i++) {
                if (typeof fn === "function" && Object.prototype.hasOwnProperty.call(this, i)) {
                    fn.call(context, this[i], i, this);
                }
            }
        };
    }

    // 讓數組中的每一個元素調用給定的函數,然後把得到的結果放到新數組中返回
    if (typeof Array.prototype.map != "function") {
        Array.prototype.map = function (fn, context) {
            var arr = [];
            if (typeof fn === "function") {
                for (var k = 0, length = this.length; k < length; k++) {
                    arr.push(fn.call(context, this[k], k, this));
                }
            }
            return arr;
        };
    }

    // 把符合條件的元素放到一個新數組中返回
    if (typeof Array.prototype.filter != "function") {
        Array.prototype.filter = function (fn, context) {
            var arr = [];
            if (typeof fn === "function") {
                for (var k = 0, length = this.length; k < length; k++) {
                    fn.call(context, this[k], k, this) && arr.push(this[k]);
                }
            }
            return arr;
        };
    }

    // 如果數組中的每個元素都能通過給定的函數的測試,則返回true,反之false
    if (typeof Array.prototype.every != "function") {
        Array.prototype.every = function (fn, context) {
            var passed = true;
            if (typeof fn === "function") {
                for (var k = 0, length = this.length; k < length; k++) {
                    if (passed === false) break;
                    passed = !!fn.call(context, this[k], k, this);
                }
            }
            return passed;
        };
    }

    // 類似every函數,但只要有一個通過給定函數的測試就返回true
    if (typeof Array.prototype.some != "function") {
        Array.prototype.some = function (fn, context) {
            var passed = false;
            if (typeof fn === "function") {
                for (var k = 0, length = this.length; k < length; k++) {
                    if (passed === true) break;
                    passed = !!fn.call(context, this[k], k, this);
                }
            }
            return passed;
        };
    }

    // 返回元素在數組的索引,沒有則返回-1,從左到右
    if (typeof Array.prototype.indexOf != "function") {
        Array.prototype.indexOf = function (item, index) {
            var n = this.length,
                i = index == null ? 0 : index < 0 ? Math.max(0, n + index) : index;
            for (; i < n; i++) {
                if (i in this && this[i] === item) {
                    return i
                }
            }
            return -1
        };
    }

    // 返回元素在數組的索引,沒有則返回-1,從右到左
    if (typeof Array.prototype.lastIndexOf != "function") {
        Array.prototype.lastIndexOf = function (item, index) {
            var n = this.length,
                i = index == null ? n-1 : index < 0 ? Math.max(0, n + index) : index;
            for (; i >= 0; i--) {
                if (i in this && this[i] === item) {
                    return i;
                }
            }
            return -1;
        };
    }

    // 讓數組元素依次調用給定函數,最後返回一個值(從左到右)
    if (typeof Array.prototype.reduce != "function") {
        Array.prototype.reduce = function (callback, initialValue) {
            var previous = initialValue, k = 0, length = this.length;
            if (typeof initialValue === "undefined") {
                previous = this[0];
                k = 1;
            }
            if (typeof callback === "function") {
                for (k; k < length; k++) {
                    this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
                }
            }
            return previous;
        };
    }

    // 讓數組元素依次調用給定函數,最後返回一個值(從右到左)
    if (typeof Array.prototype.reduceRight != "function") {
        Array.prototype.reduceRight = function (callback, initialValue) {
            var length = this.length, k = length - 1, previous = initialValue;
            if (typeof initialValue === "undefined") {
                previous = this[length - 1];
                k--;
            }
            if (typeof callback === "function") {
                for (k; k > -1; k-=1) {
                    this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
                }
            }
            return previous;
        };
    }

    // 去掉重覆項(唯一性),返回新數組
    if (typeof Array.prototype.uniq != "function") {
        Array.prototype.uniq = function() {
            var arr = [];
            arr[0] = this[0];
            for (var i = 1; i < this.length; i++) {
                if (arr.indexOf(this[i]) == -1) {
                    arr.push(this[i]);
                }
            }
            return arr;
        };
    }

    // 指定刪除數組中某值
    if (typeof Array.prototype.remove != "function") {
        Array.prototype.remove = function(item) {
            for (var i = this.length; i >= 0; i--) {
                if (item === this[i]) {
                    this.splice(i, 1);
                }
            }
            return this;
        };
    }

    // 打亂數組順序
    if (typeof Array.prototype.shuffle != "function") {
        Array.prototype.shuffle = function() {
            var i = this.length;
            while (i) {
                var j = Math.floor(Math.random()*i);
                var t = this[--i];
                this[i] = this[j];
                this[j] = t;
            }
            return this;
        };
    }

    // 求數組的最大值
    if (typeof Array.prototype.max != "function") {
        Array.prototype.max = function() {
            return Math.max.apply({}, this)
        };
    }

    // 求數組的最小值
    if (typeof Array.prototype.max != "function") {
        Array.prototype.min = function() {
            return Math.min.apply({}, this)
        };
    }

    // 判斷是否為數組
    if (typeof Array.prototype.isArray != "function") {
        Array.prototype.isArray = function() {
            return Object.prototype.toString.apply(this) === "[object Array]";
        };
    }
}());

 參考

http://www.cnblogs.com/rubylouvre/archive/2009/09/20/1570461.html

http://www.cnblogs.com/rubylouvre/archive/2009/09/16/1568123.html

http://www.cnblogs.com/rubylouvre/archive/2009/09/15/1567338.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先,在assets資源文件下放入圖標字體庫。我這兒採用的是fontawesome-webfont.ttf然後, 在安卓中載入這個資源文件TypefacefontFace = Typeface.createFromAsset(context.getAssets(), "fontawesome-web...
  • 大綱:iOS系統發展UI和OC簡單的APP程式程式的生命周期1.iOS的系統發展從1983年OC程式開始發展到2015年,30多年的時間,但這依然不是一個十分完善的語言,可以說現在都沒有一個十分完善的,不用更新了的編程語言。但是,iOS選擇了OC作為它的開發語言,這是為什麼我們前期需要先來學習OC語...
  • 釘釘深圳研發團隊 denny/2016.01.06/ [email protected]
  • 查看效果:http://hovertree.com/texiao/hoverclock/demo4.htm本插件使用方便,可以在博客園的頁面中使用,請看本頁面右側:http://www.cnblogs.com/roucheng/p/css3clock.html簡潔代碼如下:效果圖圖下:完整代碼如下:...
  • /** * Created by laixi on 2016/1/7. * Date對象擴展 */(function() { // 求當前日期與傳入的日期相隔多少天 if (typeof Date.prototype.getDateInterval != "function") { ...
  • 一、 css 選擇器1.css派生選擇器The strongly emphasized word in this paragraph isred.This subhead is also red.The strongly emphasized word in this subhead isblue....
  • 最近看視頻學習了前端自動化的一些知識,確實讓我大開眼界。感覺前端越來越神器了。同時跟著視頻自己也嘗試運用了一些工具去構建前端項目,但是中間遇見了很多坑,磕磕絆絆的才實現了一點功能,所以打算記錄一下學習過程中的筆記。首先列舉一下關鍵詞:NodeJS、Git、Yeoman、bower、Grunt。 其中...
  • /** * Created by laixiangran on 2015/12/12. * String擴展 */(function() { // 十六進位顏色值的正則表達式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; // RG...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...