Details of several new efficient array operation methods in the JAVASCRIPT,ES5 standard

Source: Internet
Author: User
Tags prev

1, JS commonly used array Object properties:

, which is marked with a red circle, and is a new attribute for ES5.

2, browser support situation:

    • ie:9+;
    • Chrome;
    • firefox2+;
    • Safari to the most;
    • Opera 9.5+;

3. Position method

The ECMASCRIPT5 defines 2 positional methods for an array. IndexOf (), lastIndexOf (); Both methods receive two parameters: the subparagraphs to find (optional) represents the index at which to find the starting point. where INDEXOF () looks backwards from the beginning of the array (position 0), lastIndexOf () looks forward from the end of the array. Both of these methods return the position of the item being looked up in the array, or return-1 if not found; Example:
varnumbers = [1,2,3,4,5,4,3,2,1];alert (Numbers.indexof (4));//4Alert (Number.lastindexof (4));//5Alert (Number.indexof (4,4));//5Alert (Number.lastindexof (4,4));//3

4. Iterative method

ECMASCRIPT5 defines 5 iterative methods for an array.

4.1,every ()

Definition and usage: theevery () method is used to detect whether all elements of an array conform to a specified condition (provided by a function).

The Every () method detects all elements in the array using the specified function:
    • If an element is detected in the array that is not satisfied, the entire expression returns false , and the remaining elements are no longer instrumented.
    • Returns true if all elements satisfy the condition.

Note: every () does not detect empty arrays.

Note: every () does not change the original array.

Description: Detects whether all elements of an array ages are greater than 18:

var ages = [];function Checkadult (age) {      return;} function MyFunction () {    document.getElementById ("demo"). InnerHTML = ages.every (Checkadult);}

The result is:

false;

4.2. Some ()
Definition and Usage: the Some () method is used to detect whether an element in an array satisfies a specified condition (provided by a function).
Runs the given function for each item in the array, and returns True if the function returns true for any of the entries;

The code is as follows:

var numbers = [1,2,3,4,5,4,3,2, 1 ]; var someresult = numbers.some (function (item,index,array) {//item refers to algebraic group values; index refers to algebraic group subscript; Array refers to the algebraic group itself;     return (item>2);}); alert (someresult);

The result is:

true;

4.3. Filter ()

Definition and Usage: the filter () method creates a new array of elements in the new array by examining all the elements in the specified array that meet the criteria. Each item in an array runs the given function, and returns a list of items for which the function returns True. Description: To return an array of all values greater than 2, the code is as follows:
var numbers = [1,2,3,4,5,4,3,2, 1 ]; var filterresult = Numbers.filter (function (item,index,array) {//item refers to algebraic group values; index refers to algebraic group subscript; Array refers to the algebraic group itself;     return (item>2);}); alert (filterresult);

The result is:

[3,4,5,4,3]

4.4. Map ()

Definition and Usage: the map () method returns a new array in which the elements of the array call the value after the function is processed by the original array element. Each item in the array runs the given function, returning an array that consists of the results of each function call. Description: Multiply each item in the array by 2 to return an array of these products, with the following code:
var numbers = [1,2,3,4,5,4,3,2, 1 ]; var mapresult = Numbers.map (function (item,index,array) {//item refers to algebraic group values; index refers to algebraic group subscript; Array refers to the algebraic group itself;     return item*2;}); alert (mapresult);

The result is:

[2,4,6,8,ten,8,6,4,2]

4.5. ForEach ()

definition and Usage: Runs the given function for each item in the array. This method has no return value. is essentially the same as iterating through an array with a for loop. The code is as follows:
var numbers = [1,2,3,4];numbers.foreach (function (Item,index, Array) {    Console.log (item);});

The result is:

1 2 3 4

5, the reduction method

ECMASCRIPT5 two new ways to reduce arrays: reduce () and reduceright ();These two methods will iterate over all the items of the algebraic group and then build a value that is ultimately returned. where the reduce () method starts with the first item in the array and traverses through to the end. The reduceright () begins with the last item in the array and traverses forward to the first item. Both methods receive two parameters: a function that is called on each item and (optionally) as the initial value for narrowing the base. the functions passed to reduce () and Reduceright () receive 4 parameters: the previous value, the current value, the index of the item, and the array object. Description: Use the reduce () method to perform an operation that evaluates the sum of all the values in the array. The code is as follows:
var values  = [1,2,3,4,5]; var sum =values.reduce (function (prev,cur,index,array) {    return  prev+  cur;}); alert (sum);

The result is:

15

Details of several new efficient array operation methods in the JAVASCRIPT,ES5 standard

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.