Javascript array operation [Z]

Source: Internet
Author: User
Tags javascript array
Function
Join (delimiter): concatenates all elements in the array into strings using delimiter.
Concat (array1, array2,...): merges arrays without affecting the original array, but returns the New merged array.
Pop (): delete and return the last element.
Push (element1, element2,...): append an element to the end of the array
Shift (): delete and return the first element.
Unshift (element1, element2,...): add elements at the beginning of the array
Reverse (): reverse the first and last order of array elements
Sort (function): sorts arrays.
If no parameter is specified, all elements are sorted by characters by character by default)
The parameter must be a function, similar to function (a, B) {}. In the function, judge the size of a and B. A> B returns a positive number, and a <B returns a negative number, A = B returns 0
Slice (START, end): returns a new array, and copies the elements between start and end (excluding end) to the new array.
Splice (START, Count, replaceelement1, replaceelement2,...): delete or replace array elements, and delete or replace count elements from start.
If the replaceelement parameter is provided, replace it. Otherwise, delete the replaceelement. The number of replaced elements and replaceelements do not have to be equal.
Returns an array containing the deleted or replaced elements.

Method to determine whether an array is used
VaR arrayvar = ["AAA", "BBB", "CCC"];
VaR nonarrayvar = {length: 4, otherattribute: "ATTR "};
Document. Write (arrayvar. constructor = array );
Document. Write ("<br/> ");
Document. Write (nonarrayvar. constructor = array );
The result is true or false.

Grep, map
This method is used when jquery implements selector.
Grep
Grep operations on arrays refer to operations similarCode:
Array newarray = grep (condition, oldarray). This operation traverses each element of the oldarray array. If the current processing element meets the condition, it is added to the returned array.
The grep in jquery is similar to this: function (elems, callback), elems is a collection of DOM elements, and callback is an expression function that implements selector, if callback returns true, it indicates that the currently processed element complies with the selector expression.
Map
Unlike grep, map only performs the specified operation on each element of the array without any conditional judgment, that is, it modifies a loop process.

If you like the dynamic language style, you can add grep and map methods to the array, for example: Array. Prototype. grep =   Function (Condition, operation, inverse ){
If (Arguments. Length < 1 ) Return [];

VaR Tester;
If ( Typeof Condition = " Function " ) Tester = Condition;
Else   If (Condition. Constructor = Regexp) Tester =   Function (E, I ){ Return Condition. Test (E) ;};
Else   If ( Typeof Condition = " String " )
Try {
Tester = Eval ( " False | function (E, I) {return "   + Condition +   " ;} " );
} Catch (E ){
}
Else Tester =   Function (E, I ){ Return   False ;};
If ( ! Tester) Return [];

VaR Command;
If ( Typeof Operation = " Function " ) Command = Operation;
Else   If ( Typeof Operation = " String " )
Try {
Command = Eval ( " False | function (E, I ){ "   + Operation +   " ; Return e ;} " );
} Catch (E ){
}
If ( Typeof Command ! = " Function " ) Command = Function (E, I ){ Return E ;};

VaR Result = [], Match, T;
For ( VaR I = 0 ; I < This . Length; I ++ ){
Match = Tester ( This [I], I );
If (MATCH) T = Command ( This [I], I );
If (Match &&   ! Inverse && T ! = Null ) T. Constructor = Array ? Result. Concat (t): result. Push (t );
Else   If ( ! Match && Inverse) result. Push ( This [I]);
}

Return Result;
};
Array. Prototype. Map =   Function (Callback ){
If ( ! Callback |   Typeof Callback ! = " Function " ) Return   This ;
For ( VaR I = 0 ; I < This . Length; I ++ ) Callback ( This [I], I );
Return   This ;
};

Example: VaR Oldarray = [ 2 , 7 , 8 , 5 , 1 , 9 , 2 , 6 , 0 ];
VaR Newarray1 = Oldarray. grep ( " I % 2 = 0 " ); // Returns all elements with an even index.
VaR Newarray2 = Oldarray. grep ( " E % 2 = 1 " ); // Returns all elements whose values are odd.
Document. Write (newarray1 +   " <Br/> " ); // 2, 8, 1, 2, 0
Document. Write (newarray2 +   " <Br/> " ); // 7, 5, 1, 9

Oldarray = [ " AA " , " Bbbb " , " Abcdefg " , " CCCCC " , " 111121111 " , " 999999999 " ];
VaR Newarray3 = Oldarray. grep ( / (\ W) \ 1 {4 ,} / );
Document. Write (newarray3 +   " <Br/> " ); // Ccccccc, 999999999

Typical jquery code: VaROBJ=Document. createelement ("Ul");
OBJ. innerhtml= '<Li> aaaaaa </LI> <li style = "display: none;"> bbbbbb </LI> <li> cccccc </LI> <li> dddddddd </LI>';
Document. Body. appendchild (OBJ );

Window. setinterval (Function (){
// Convert nodelist to an array
VaR Lilist = OBJ. getelementsbytagname ( " Li " );
VaR Liarray = [];
For ( VaR I = 0 ; I < Lilist. length; I ++ ) Liarray. Push (lilist [I]);
// Use of the example grep
Liarray
. Grep ( ' E. Style ["display"] = "NONE" ' , ' E. Style ["display"] = ""; ' , True )
. Map ( Function (E, I) {e. Style [ " Display " ] =   " None " ;});
}, 1500 );

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.