JS数组字符串方法扩展

今天整理了一些JS数组字符串的扩展方法,大部分是自己写的,一小部分来源于网络,也进行了适当优化。

Ps:数组的操作大部分都返回了this,因此可进行‘迷人的’链式调用;本例中[].map(func), 若需对数组的每一项进行某种相同操作,func需要有返回值

压缩后:

!function(){Array.prototype.cut=function(){return this.length>=+arguments[0]&&(this.length=+arguments[0]),this};Array.prototype.getLast=function(){return this.slice(-1)};Array.prototype.combine=function(){return !!arguments[0].cut()&&this.push.apply(this,arguments[0]),this};Array.prototype.each=function(fn){for(var i=0,n=this.length;i<n;i++){fn(i,this[i])}return this};Array.prototype.map=function(fn){var o=[];this.each(function(index,item){"function"==typeof fn?o[index]=fn(index,item):o[index]=item});return o};Array.prototype.changeType=function(flag){var o={};this.each(function(index,item){o[index]=item});return !!flag?o:JSON.stringify(o)};Array.prototype.distinct=function(){o=[];for(i=0;i<this.length;i++){o.indexOf(this[i])>-1?1:o.push(this[i])}return o};Array.prototype.parseNodelist=function(){return[].slice.call((arguments[0][0] instanceof HTMLElement)?arguments[0]:[])};Array.prototype.shuffle=function(){var o=[];o=this.sort(function(){return Math.random()-0.5});return o};Array.prototype.remove=function(){var index=this.indexOf(arguments[0]);while(index>-1){this.splice(index,1);index=this.indexOf(arguments[0])}return this};Array.prototype.sortNum=function(f){if(!f){f=0}if(f==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]};String.prototype.ToCharArray=function(){return this.split("")};String.prototype.Repeat=function(num){var tmpArr=[];for(var i=0;i<num;i++){tmpArr.push(this)}return tmpArr.join("")};String.prototype.Reverse=function(){return this.split("").reverse().join("")};String.prototype.IsNumeric=function(){var tmpFloat=parseFloat(this);if(isNaN(tmpFloat)){return false}var tmpLen=this.length-tmpFloat.toString().length;return tmpFloat+"0".Repeat(tmpLen)==this};String.prototype.IsInt=function(){if(this=="NaN"){return false}return this==parseInt(this).toString()};String.prototype.resetBlank=function(){return this.replace(/s+/g," ")};String.prototype.LTrim=function(){return this.replace(/^s+/g,"")};String.prototype.RTrim=function(){return this.replace(/s+$/g,"")};String.prototype.trim=function(){return this.replace(/(^s+)|(s+$)/g,"")};String.prototype.getNum=function(){return this.replace(/[^d]/g,"")};String.prototype.getEn=function(){return this.replace(/[^A-Za-z]/g,"")};String.prototype.getCn=function(){return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g,"")};String.prototype.getRealLength=function(){return this.replace(/[^x00-xff]/g,"--").length};String.prototype.left=function(n){return this.slice(0,n)};String.prototype.right=function(n){return this.slice(this.length-n)};String.prototype.HTMLEncode=function(){var re=this;var q1=[/x26/g,/x3C/g,/x3E/g,/x20/g];var q2=["&","<",">"," "];for(var i=0;i<q1.length;i++){re=re.replace(q1[i],q2[i])}return re};String.prototype.ascW=function(){var strText="";for(var i=0;i<this.length;i++){strText+="&#"+this.charCodeAt(i)+";"}return strText}}();


源码:

 ;!function () {
    //截断数组
        Array.prototype.cut = function () { return this.length >= +arguments[0] && (this.length = +arguments[0]), this; };
        //获取最后一个元素
            Array.prototype.getLast = function () { return this.slice(-1); };
            //合并两个数组
        Array.prototype.combine = function () {
            return !!arguments[0].cut() &&
        this.push.apply(this, arguments[0]), this;
        };
        //遍历数组
        Array.prototype.each = function (fn) {

            for (var i = 0, n = this.length; i < n; i++) {
                fn(i, this[i]);
            }
            return this;
        };
        //传function类型 可对每一项进行操作,传其它类型克隆一个数组
        Array.prototype.map = function (fn) {
            var o = [];
            this.each(function (index, item) {
                'function' == typeof fn ? o[index] = fn(index, item) : o[index] = item;
            });
            return o;
        };
        //传 !!1 返回对象 传 !1返回字符串
        Array.prototype.changeType = function (flag) {
            var o = {};
            this.each(function (index, item) {
                o[index] = item;
            });
            return !!flag ? o : JSON.stringify(o);
        };
        //去重
        Array.prototype.distinct = function () {
            o = [];
            for (i = 0; i < this.length; i++) {
                o.indexOf(this[i]) > -1 ? 1 : o.push(this[i]);
            }
            return o;

        };
        //Dom节点共享Array的方法
        Array.prototype.parseNodelist = function () {
            return [].slice.call((arguments[0][0] instanceof HTMLElement) ? arguments[0] : []);
        };
        //随机排序
        Array.prototype.shuffle = function () {
            var o = []; o = this.sort(function () { return Math.random() - 0.5 });
            return o;
        };
        //删除某个元素
        Array.prototype.remove = function () {
            var index = this.indexOf(arguments[0]);
            while (index > -1) {
                this.splice(index, 1);
                index = this.indexOf(arguments[0]);
            }
            return this;
        };
        //排序 1降 0升
        Array.prototype.sortNum = function (f) {
            if (!f) f = 0;
            if (f == 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];
        }

        //=======================string
        //获取字符数组 
        String.prototype.ToCharArray = function () {
            return this.split("");
        }
        //获取N个相同的字符串 
        String.prototype.Repeat = function (num) {
            var tmpArr = [];
            for (var i = 0; i < num; i++) tmpArr.push(this);
            return tmpArr.join("");
        }
        //逆序 
        String.prototype.Reverse = function () {
            return this.split("").reverse().join("");
        }
        //测试是否是数字 
        String.prototype.IsNumeric = function () {
            var tmpFloat = parseFloat(this);
            if (isNaN(tmpFloat)) {return false};
            var tmpLen = this.length - tmpFloat.toString().length;
            return tmpFloat + "0".Repeat(tmpLen) == this;
        }
        //测试是否是整数 
        String.prototype.IsInt = function () {
            if (this == "NaN") {return false};
            return this == parseInt(this).toString();
        }
        // 合并多个空白为一个空白 
        String.prototype.resetBlank = function () {
            return this.replace(/s+/g, " ");
        }
        // 除去左边空白 
        String.prototype.LTrim = function () {
            return this.replace(/^s+/g, "");
        }
        // 除去右边空白 
        String.prototype.RTrim = function () {
            return this.replace(/s+$/g, "");
        }
        // 除去两边空白 
        String.prototype.trim = function () {
            return this.replace(/(^s+)|(s+$)/g, "");
        }
        // 保留数字 
        String.prototype.getNum = function () {
            return this.replace(/[^d]/g, "");
        }
        // 保留字母 
        String.prototype.getEn = function () {
            return this.replace(/[^A-Za-z]/g, "");
        }
        // 保留中文 
        String.prototype.getCn = function () {
            return this.replace(/[^u4e00-u9fa5uf900-ufa2d]/g, "");
        }
        // 得到字节长度 
        String.prototype.getRealLength = function () {
            return this.replace(/[^x00-xff]/g, "--").length;
        }
        // 从左截取指定长度的字串 
        String.prototype.left = function (n) {
            return this.slice(0, n);
        }
        // 从右截取指定长度的字串 
        String.prototype.right = function (n) {
            return this.slice(this.length - n);
        }
        // HTML编码 
        String.prototype.HTMLEncode = function () {
            var re = this;
            var q1 = [/x26/g, /x3C/g, /x3E/g, /x20/g];
            var q2 = ["&", "<", ">", " "];
            for (var i = 0; i < q1.length; i++)
                re = re.replace(q1[i], q2[i]);
            return re;
        }
        // Unicode转化 
        String.prototype.ascW = function () {
            var strText = "";
            for (var i = 0; i < this.length; i++) strText += "&#" + this.charCodeAt(i) + ";";
            return strText;
        } 
    } ();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值