Js array does not support map filter and other solutions. arraymap

Source: Internet
Author: User

Js array does not support map filter and other solutions. arraymap


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 = [, 'Blue ', 30, '35', 'ball', 'bucket'];
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 abve 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 change the sorting 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 = [,];
Var yourArray = [,];
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 var a = new Array ("5", "7", "7"); a. unique ();
Array. prototype. unique = function (){
Var data = this | [];
Var a ={}; // declare an object. javascript objects can be used as hash tables.
For (var I = 0; I <data. length; I ++ ){
A [data [I] = true; // sets the flag to use the value of the array as the subscript, so that repeated values can be removed.
}
Data. length = 0;

For (var I in a) {// traverses the object and restores the marked objects to an 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 an element based on the subscript of the array.
*/
Array. prototype. removeByIndex = function ($ n ){
If ($ n <0) {// if n <0, no operation is performed.
Return this;
} Else {
Return this. slice (0, $ n). concat (this. slice ($ n + 1, this. length ));
}
}
// Dependent 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. The key word is shallow copy. If an array is encountered, 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); // The parameter is not directly processed, but copied.
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.