JS Array Extension instance code

Source: Internet
Author: User



Array. prototype. del = function (n)
{
If (n <0) return this;
Return this. slice (0, n). concat (this. slice (n + 1, this. length ));
}
// Shuffling the Array
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;
}
// Sort the number Array
Array. prototype. sortNum = function (f)
{
If (! F) f = 0;
If (f = 1) return this. sort (function (a, B) {return B-;});
Return this. sort (function (a, B) {return a-B ;});
}
// Obtain the numeric entry of the number Array
Array. prototype. getMax = function ()
{
Return this. sortNum (1) [0];
}
// Obtain the minimum entry of the number Array
Array. prototype. getMin = function ()
{
Return this. sortNum (0) [0];
}
// The position where the specified Element value appears for the first time in the array
Array. prototype. indexOf = function (o)
{
For (var I = 0; I <this. length; I ++) if (this [I] = o) return I;
Return-1;
}
// Remove repeated items from the array
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 "wo" from the array"
Var newArr = arr. del (1 );
Returns the position where "me" appears for the first time in the array. If not,-1 is returned.

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.