JS Array ECMAScript5 Methods

Source: Internet
Author: User

In the new version of JavaScript (ECMAScript 5), several new methods have been added to the array. These methods include:

ForEach (f [, O]): This method is similar to the for/in loop, which is used to traverse the entire array and perform certain operations on the function, but it does not traverse the properties of the array. It accepts a function parameter, which can have 1, 2, and 3 parameters. The complete three parameters are: Each value in the array, the index of the array, and the array itself. This function has no return value. Such as:

var a = [0,1,2,3,4];   var sum = 0;  A.foreach (function(v,i,arr) {       + = V;       Console.log (Arr[i]);   // http://www.cnblogs.com/roucheng/

Map (f [, O]): Similar to the foreach () method, but it has a return value---a new array, its argument function must have a return value, the return value of the parameter function as the element of the new array. Other usages are similar to the foreach () method.

var a = [1,2,3,4,5];   var b = A.map (function(x) {       return x*x;  });     // [1,2,3,4,5]   // [1,4,9,16,25]

Filter (f [, O]): Filters the array elements and returns a new set of matching requirements. Such as:

var a = [n/a];  a[5] = 6;     var b = A.filter (function(x) {      return x!== undefined;  });     // [1,2,3,undefined,undefined,6];   // [1,2,3,6]; // http://www.cnblogs.com/roucheng/

Every (f [, O]): There is a Boolean type of return value, and the function parameter F is used to detect whether an array element satisfies the requirement. True is returned if all elements of the array satisfy the requirements of f, otherwise false is returned.

var a = [1,3,7];     var flag = A.every (function(x) {       return x < 5;  });     // false

Some (f [, O]): Unlike the Every () method, returns True if an array element satisfies the requirements of f, otherwise false.

var a = [1,3,7];     var flag = A.some (function(x) {       return x < 5;  });     // true

Reduce (f [, initial, index, array]): Calculates the value of an array element. Returns the value of the last calculation. The function f (x, Y) accepts two parameters, if there is an initial value, the initial value is calculated as X, and then each time the calculated value as the next X, the array elements in turn as Y.

var a = [1,2,3,4,5];     var b = a.reduce (function(x, y       ) {return x+y;  },0);    Console.log (b);   //  the

Reduceright (): The same as the reduce () function, except that it takes an array element from right to left.

IndexOf (value [, start]): finds the corresponding index position in the array based on the value, takes multiple match indexes with the smallest one, or returns 1 without a match.

LastIndexOf (value [, start]): Similar to the indexof () function, except that it searches from behind to the front.
Recommendation: http://www.cnblogs.com/roucheng/p/css3train.html

JS Array ECMAScript5 Methods

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.