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");
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