JavaScript array Delete sort move divisor group repeat

Source: Internet
Author: User
Tags numeric javascript array

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;
}
Related Article

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.