Methods of array arrays in JavaScript

Source: Internet
Author: User
Tags array length

Find: IndexOf, lastIndexOf

Iterations: Every, filter, ForEach, map, Somereduce, Reduceright

Usage:

1 /*2 1 Find a method:3 * Arr.indexof (value)4 * One parameter returns the position subscript of the array where value is located5 * Return when not found-16  * 7 * Arr.indexof (start, value)8 * Find value from start to return subscript9 * Return when not found-1Ten  *  One * Arr.lastindexof (value) A * return to subscript from the rear -  *  -  *  the  *  - * * Iteration method: - * Arr.every (function (item, index, arr) { - * Return item > 2; Returns true if each item is greater than 2 otherwise false +  * })  - * An incoming function executes a function on each item, + * Returns TRUE if each entry returns True A * If there is a return false, then the return is false. at  *  - * Arr.filter (function (item, index, arr) { - * Return item > 2; Returns an array element that is greater than 2 -  * } )  - * Pass in a function for each element to execute - * The result is true and the element returns in  *          - * Arr.foreach (function (item, index, arr) { to * Alert (item) +  * }) - * Provides a loop method the * Each element performs a function *  *  $ * ARR.MAP (function (item, index, arr) {Panax Notoginseng * Return item * 2; -  * }) the * Performs a function on each item of ARR and returns the result of execution +  *  A  *  the * Arr.some (function (Item,index,arr) { +  *      - * RETRN item > 2; $  * }) $ * to judge each element in Arr, - * There is a return true and finally returns True - * If all is false, then return false the  *  -  * Wuyi  *  the * Arr.reduce (function (prev, cur, index, array) { - *//Money A value current value index position array Wu * return prev + cur; -  * }) About * Each element in the array is added and finally returned $  *  -  *  -  *  - * Arr.reduceright () A * The array is executed from right to left to return the final result +  *  the  *  -  *  $  *  the  *  the  *  the  *  the  *  -  * */

Add: Push, pop, shift, Unshift

Intercept Splice, Slice

Action: concat, join, reverse, sort

Usage:

1 //how arrays are created2 //1 Not recommended3 //var arr = new Array ();4 //2 recommended JS array internal data types can be different5 vararr = [1,2,3,4,true, "ABC",NewDate ()];6 //alert (arr.length);7 //JS Array can arbitrarily change the length of the element type arbitrary8Arr.length = 5;9 alert (arr);Ten  One /* A * Common methods for arrays - * 1 Array Add delete element - * result = Arr.push (Value1,value2,value3,....) adds an element to an array returns the value of the new array's length the * result = Arr.pop () returns the element at the tail of the array and removes -  *      - * result = Arr.shift () removes an element from the head and returns the head element - * result = Arr.unshift (A,b,c,d,e ...) Insert some elements from the head returns the new array length +  *  - * 2 Array intercept + * Arr.splice (1,2,3,4,5) A * Methods for intercepting arrays: Changing the array itself at * The first parameter indicates the starting position - * The second parameter indicates the number of items to intercept - * After the third parameter, as a subarray, the array is inserted from the intercepted position - * Arr.splice (1) 1 after the elements are all cut off - * Arr.splice Two elements starting from the 1th subscript element -  *  in  *   - * result = Arr.slice (2,3) to * Intercept operation the first parameter is the starting point of interception, the second parameter is the end left closed right open intercept + * Returns the intercepted result without changing the array itself -  *  the  *  * * 3 Array operation method $  *  Panax Notoginseng * result = Arr1.concat (ARR2) - * arr1 and arr2 are stitched back the * Do not change the array itself +  *   A * result = Arr.join ("-") the * Add a-symbol between each element and return +  *  - * Arr.reverse () reverse array order $ * Change the array itself $  *  - * Arr.sort () positive order - * But compare each value by one byte of the string the * For example, 10 and 4 compare, will think 1:4 small 10 will be ranked in front - * Change the array itselfWuyi * If you want the correct sort you need to write a method to pass in sort () the * For example - * Function Compare (value1, value2) { Wu * if (value1 < value2) { - * RETURN-1; About *}else if (value1 > value2) { $ * return 1; - *}else{ - * return 0; -  *         }     A  *     } + * Then call Arr.sort (Compare) the * So we can sort the normal sequence we want . -  *  $  * */

Methods of array arrays in JavaScript

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.