JS Array does not support the solution of map filter, etc.

Source: Internet
Author: User


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;});
}
<!--
var tmp = [5,9,12,18,56,1,10,42, ' Blue ', ' 7,97,53,33,30,35,27,30 ', ' a ', ' 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 (first letter 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;
}
Randomly changing the ordering of 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 duplicate values from the array var a = new Array ("5", "7", "7"); A.unique ();
Array.prototype.unique = function () {
var data = This | | [];
var a = {}; Declares an object, a JavaScript object that can be used as a hash table
for (var i = 0; i < data.length; i++) {
A[data[i]] = true; Set the tag to mark the value of the array so that duplicate values can be removed
}
data.length = 0;

for (var i in a) {//iterate over the object, put the marked Restore array
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 in this &&
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));
}
}
Dependent 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, the 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 of the parameters, but a copy of the
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)));
}

JS Array does not support the solution of map filter, etc.

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.