The extension function code _javascript technique of JS Array object

Source: Internet
Author: User
Tags shallow copy

Use

Copy Code code as follows:

<script language=javascript>
var isnumeric = function (x) {
Returns True if X is numeric and false if it isn't.
var RegExp =/^ (-)? (\d*) (\.?) (\d*) $/;
return String (x). Match (REGEXP);
}
var myarray = [1, ' Two ', 3, ' Four ', 5, ' Six ', 7, ' eight ', 9, ' Ten '];
var oddarray=myarray.filter (IsNumeric); outputs:1,3,5,7,9
var oddarray=myarray.some (IsNumeric); Outputs:true
var oddarray=myarray.every (IsNumeric); Outputs:false
var printArray =function (x, idx) {
Document.writeln (' [' +idx+ '] = ' +x ');
}
Myarray.foreach (PrintArray);//outputs: [0] = 1 [1] = two [2] = 3 [3] = four [4] = 5
Myarray.remove (9);
Document.writeln (myarray);

Copy Code code as follows:

if (! Array.prototype.every)
{
Array.prototype.every = function (Fun/*, thisp*/)
{
var len = this.length;
if (typeof fun!= "function")
throw new TypeError ();

var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
!fun.call (Thisp, this[i], I, this))
return false;
}

return true;
};
}
if (! Array.prototype.filter)
{
Array.prototype.filter = function (Fun/*, thisp*/)
{
var len = this.length;
if (typeof fun!= "function")
throw new TypeError ();

var res = new Array ();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; In case fun mutates this
if (Fun.call (Thisp, Val, I, this))
Res.push (Val);
}
}

return res;
};
}
if (! Array.prototype.forEach)
{
Array.prototype.forEach = function (Fun/*, thisp*/)
{
var len = this.length;
if (typeof fun!= "function")
throw new TypeError ();

var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
Fun.call (Thisp, this[i], I, this);
}
};
}
if (! ARRAY.PROTOTYPE.MAP)
{
Array.prototype.map = function (Fun/*, thisp*/)
{
var len = this.length;
if (typeof fun!= "function")
throw new TypeError ();

var res = new Array (len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
Res[i] = Fun.call (Thisp, this[i], I, this);
}

return res;
};
}
if (! Array.prototype.some)
{
Array.prototype.some = function (Fun/*, thisp*/)
{
var len = this.length;
if (typeof fun!= "function")
throw new TypeError ();

var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
Fun.call (Thisp, this[i], I, this))
return true;
}

return false;
};
}
Array.prototype.sortNum = function () {
Return This.sort (function (a,b) {return a-b;});
}
<!--
var tmp = [5,9,12,18,56,1,10,42, ' Blue ', ' 7,97,53,33,30,35,27,30, ' ', ' Ball ', ' bubble '];
var thirty=tmp.find (30); Returns 9, 14, 17
var thirtyfive=tmp.find (' 35 '); Returns 18
var thirtyfive=tmp.find (35); Returns 15
var haveblue=tmp.find (' Blue '); Returns 8
var notfound=tmp.find (' not there! '); Returns false
var regexp1=tmp.find (/^b/); Returns 8,20 (starts with B)
var regexp1=tmp.find (/^b/i); Returns 8,19,20 (same as above but ignore case)
-->
Array.prototype.find = function (searchstr) {
var returnarray = false;
For (i=0 i<this.length; i++) {
if (typeof (searchstr) = = ' function ') {
if (Searchstr.test (This[i])) {
if (!returnarray) {returnarray = []}
Returnarray.push (i);
}
} else {
if (THIS[I]===SEARCHSTR) {
if (!returnarray) {returnarray = []}
Returnarray.push (i);
}
}
}
return returnarray;
}
Ordering random changes in arrays
Array.prototype.shuffle = function () {
For (Var rnd, TMP, i=this.length; i; Rnd=parseint (Math.random () *i), Tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
return this;
}
<!--var myarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var yourarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
Document.writeln (Myarray.compare (Yourarray)); Outputs:true;-->
Array.prototype.compare = function (Testarr) {
if (this.length!= testarr.length) return false;
for (var i = 0; i < testarr.length; i++) {
if (this[i].compare) {
if (!this[i].compare (Testarr[i])) return false;
}
if (This[i]!== testarr[i]) return false;
}
return true;
}
Remove the duplicate value in the array var a = new Array ("5", "7", "7"); A.unique ();
Array.prototype.unique = function () {
var data = This | | [];
var a = {}; Declares an object that JavaScript objects can use as a hash table
for (var i = 0; i < data.length; i++) {
A[data[i]] = true; Sets the tag, and the value of the array is now labeled so that the duplicate values can be removed
}
data.length = 0;

for (var i in a) {//Traverse object, put marked restore to a group
This[data.length] = i;
}
return data;
}

Array.prototype.addAll = function ($array)
{
if ($array = = NULL | | $array. length = 0)
Return

for (var $i =0; $i < $array. length; $i + +)
This.push ($array [$i]);
}

Array.prototype.contains = function ($value)
{
for (var $i =0; $i <this.length; $i + +)
{
var $element = this[$i];
if ($element = = $value)
return true;
}

return false;
}

Array.prototype.indexOf = function ($value)
{
for (var $i =0; $i <this.length; $i + +)
{
if (this[$i] = = $value)
return $i;
}

return-1;
}
if (! ARRAY.PROTOTYPE.LASTINDEXOF)
{
Array.prototype.lastIndexOf = function (ELT/*, from*/)
{
var len = this.length;

var from = number (arguments[1]);
if (isNaN (from))
{
from = len-1;
}
Else
{
From = (From < 0)
? Math.ceil (from)
: Math.floor (from);
if (from < 0)
from + = Len;
else if (from >= len)
from = len-1;
}

for (; from >-1; from--)
{
if (from &&
This[from] = = ELT)
return from;
}
return-1;
};
}
Array.prototype.insertAt = function ($value, $index)
{
if ($index < 0)
This.unshift ($value);
else if ($index >= this.length)
This.push ($value);
Else
This.splice ($index, 0, $value);
}
/**
* Delete elements according to the subscript of the array
*/
Array.prototype.removebyindex=function ($n) {
if ($n <0) {//If n<0, no action is made.
return this;
}else{
Return This.slice (0, $n). Concat (This.slice ($n +1,this.length));
}
}
Reliance on IndexOf
Array.prototype.remove = function ($value)
{
var $index = This.indexof ($value);

if ($index!=-1)
This.splice ($index, 1);
}

Array.prototype.removeAll = function ()
{
while (This.length > 0)
This.pop ();
}

Array.prototype.replace = function ($oldValue, $newValue)
{
for (var $i =0; $i <this.length; $i + +)
{
if (this[$i] = = $oldValue)
{
this[$i] = $newValue;
Return
}
}
}

Array.prototype.swap = function ($a, $b)
{
if ($a = = $b)
Return

var $tmp = this[$a];
this[$a] = this[$b];
this[$b] = $tmp;
}
Array.prototype.max = function () {
Return Math.max.apply ({}, this);
}
Array.prototype.min = function () {
Return Math.min.apply ({}, this);
}
Array.prototype.splice = function (Start, Dellen, item) {
var len =this.length;
Start = start<0?0:start>len?len:start?start:0;
dellen=dellen<0?0:dellen>len?len:dellen?dellen:len;

var arr =[],res=[];
var iarr=0,ires=0,i=0;

for (i=0;i<len;i++) {
if (i<start| | ires>=dellen) arr[iarr++]=this[i];
else {
Res[ires++]=this[i];
if (Item&&ires==dellen) {
Arr[iarr++]=item;
}
}
}
if (Item&&ires<dellen) Arr[iarr]=item;

for (Var i=0;i<arr.length;i++) {
This[i]=arr[i];
}
This.length=arr.length;
return res;
}
Array.prototype.shift = function () {if (!this) Return[];return this.splice (0,1) [0];}

Add separately, keyword shallow copy, if you encounter an array, copy the elements in the array
Array.prototype.concat = function () {
var i=0;
while (I<arguments.length) {
if (typeof arguments[i] = = = ' object ' &&typeof arguments[i].splice = = ' function ' &&!arguments[i]. propertyIsEnumerable (' length ')) {
Not shallow COPY BELOW
Array.prototype.concat.apply (this,arguments[i++]);
var j=0;
while (j<arguments[i].length) This.splice (this.length,0,arguments[i][j++]);
i++;
} else{
This[this.length]=arguments[i++];
}
}
return this;
}

Array.prototype.join = function (separator) {
var i=0,str= "";
while (i<this.length) Str+=this[i++]+separator;
return str;
}

Array.prototype.pop = function () {return this.splice (this.length-1,1) [0];}

Array.prototype.push = function () {
Array.prototype.splice.apply (This,
[This.length,0].concat (Array.prototype.slice.apply (arguments))]; There is no direct processing parameter, but a copy
return this.length;
}
Array.prototype.reverse = function () {
for (Var i=0;i<this.length/2;i++) {
var temp = this[i];
This[i]= This[this.length-1-i];
This[this.length-1-i] = temp;
}
return this;
}
Array.prototype.slice = function (start, end) {
var len =this.length;
start=start<0?start+=len:start?start:0;
End =end<0?end+=len:end>len?len:end?end:len;

var I=start;
var res = [];
while (I<end) {
Res.push (this[i++]);
}
return res;
}
Arr.unshift (Ele1,ele2,ele3 ...)
Array.prototype.unshift =function () {
Array.prototype.splice.apply (This,[0,0].concat (Array.prototype.slice.apply (this,arguments)));
}

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.