Collection of some array and string prototype object extensions to implement code _javascript tips

Source: Internet
Author: User
Tags extend
From the worry-free script collected, some really good, more practical, worth collecting.
methods to extend the prototype object of an array
Copy Code code as follows:

Delete data in an array
Array.prototype.del = function (n)
{
if (n<0) return this;
Return This.slice (0,n). Concat (This.slice (n+1,this.length));
}
Array Shuffle
Array.prototype.random = function ()
{
var nr=[], me=this, t;
while (me.length>0)
{
Nr[nr.length] = Me[t = Math.floor (Math.random () * me.length)];
me = Me.del (t);
}
return nr;
}
Sorting numeric arrays
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;});
}
Get the largest item in a numeric array
Array.prototype.getMax = function ()
{
return This.sortnum (1) [0];
}
Get the smallest item of a numeric array
Array.prototype.getMin = function ()
{
Return This.sortnum (0) [0];
}
The first occurrence of an array where the specified element value occurs
Array.prototype.indexOf = function (o)
{
For (Var i=0. i<this.length; i++) if (this[i]==o) return i;
return-1;
}
To move a duplicate item in a divisor group
Array.prototype.removerepeat=function ()
{
This.sort ();
var rs = [];
var cr = false;
for (var i=0; i<this.length; i++)
{
if (!CR) CR = This[i];
else if (Cr==this[i]) rs[rs.length] = i;
else CR = This[i];
}
var re = this;
for (var i=rs.length-1 i>=0; i--) re = Re.del (rs[i));
return re;
}

Example:
var arr=["ni", "Wo", "ta"];
Delete the "Wo" in the array
var newarr=arr.del (1);
Returns the position of the first occurrence of "me" in the array, if not, returns-1
var strpos=arr.indexof ("Me");

methods to extend the prototype object of a string
Copy Code code as follows:

Get an array of characters
String.prototype.tochararray=function ()
{
Return This.split ("");
}
Get n the same string
String.prototype.repeat=function (num)
{
var tmparr=[];
for (Var i=0;i<num;i++) Tmparr.push (this);
Return Tmparr.join ("");
}
Reverse
String.prototype.reverse=function ()
{
Return This.split (""). Reverse (). Join ("");
}
Test whether it is a number
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;
}
Test whether it is an integer
String.prototype.isint=function ()
{
if (this== "NaN") return false;
Return This==parseint (This). ToString ();
}
Merge multiple blanks into one blank
String.prototype.resetBlank = function ()
{
Return This.replace (/s+/g, "");
}
Remove left blank
String.prototype.LTrim = function ()
{
Return This.replace (/^s+/g, "");
}
Remove Right blank
String.prototype.RTrim = function ()
{
Return This.replace (/s+$/g, "");
}
Remove both sides of white space
String.prototype.trim = function ()
{
Return This.replace (/(^s+) | ( s+$)/g, "");
}
Keep numbers
String.prototype.getNum = function ()
{
Return This.replace (/[^d]/g, "");
}
Keep Letters
String.prototype.getEn = function ()
{
Return This.replace (/[^a-za-z]/g, "");
}
Keep Chinese
String.prototype.getCn = function ()
{
Return This.replace (/[^u4e00-u9fa5uf900-ufa2d]/g, "");
}
Get byte length
String.prototype.getRealLength = function ()
{
Return This.replace (/[^x00-xff]/g, "--"). Length;
}
To intercept a string of a specified length from the left
String.prototype.left = function (n)
{
Return This.slice (0,n);
}
To intercept a string of a specified length from the right
String.prototype.right = function (n)
{
Return This.slice (THIS.LENGTH-N);
}
HTML encoding
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 conversion
String.prototype.ascW = function ()
{
var strText = "";
for (var i=0; i<this.length; i++) StrText + = "&#" + this.charcodeat (i) + ";";
return strText;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.