ECMASCRIPT5 provides 9 new array methods: Traverse, map, filter, detect, simplify, and search arrays

Source: Internet
Author: User

The first parameter of most methods receives a function and is called once for each element of the array. If it is a sparse array, the passed function is not called on elements that do not exist. In most cases, calling the supplied function uses three parameters: the array element, the index of the element, the array itself, usually providing only the first argument, ignoring the latter two arguments.

The second argument is optional, and if there is a second argument, the called function is treated as a method of the second argument, that is, using this in the calling function represents the second argument.

Array methods do not modify the original array that they call. Of course, the functions passed to these methods can modify these arrays.

ForEach ():

method to iterate through the array and invoke the specified function for each element .

Note: You cannot terminate a traversal before all elements are passed to the called function (that is, you cannot break or return).

Map ():

Each element of the array is passed to the specified function and returns an array that contains the return value of the function.

Filter ():

The array element returned by the method is a subset of the called Array. The transfer function is used to logically determine that the function returns TRUE or FALSE. Filter skips the missing element in the sparse array, and its return array is always dense, that is, the empty array of compressed sparse:

var dense = sparse.filter (function () {return false});

Compress the vacancy and delete the undefined and null elements, so you can use filter ():

  A  = A.filter (functionreturn x!=undefined && x!=null});

Every () and some ():

Is the logical decision of arrays: They apply the specified function to the array element to determine, which returns TRUE or False

The Every () method is like "for All" in mathematics: Returns True if and only if all elements in the array call the decision function.

The Some () method is like the "existential" quantifier in mathematics, when at least one element in the array is called the decision function returns TRUE. The decision function returns TRUE. And only if all the elements in the numeric value return FALSE, it returns false.

Reduce () and reduceright ():

Uses the specified function to combine array elements to produce a single value. This is a common operation in functional programming, and can also be referred to as "injection" and "folding" as follows:

var a = [1,2,3,4,5]; var sum = a.reduce (function(x, y) {return/// array sum var product = A.reduce (function(x, y) {return// array-quadrature var max = A.reduce (  function(x, y) {return// seek maximum value

reduce () requires two parameters. The first is a function that performs a simplified operation. The task of simplifying a function is to use some method to combine or simplify two values into a value, and return the value after simplification. The Second optional parameter is an initial value that is passed to the function. The simplified operation function, unlike the one used by the foreach and map mentioned above, is familiar with the "array element", "Index of the Element", and "array itself" passed as the 2nd to 4th argument to the function. The first parameter is the cumulative result of the simplification operation so far. The first time a function is called, the first parameter is the second argument passed to reduce (). As described above, Max is not used with the second parameter (that is, no initial value is specified). When you call reduce () without specifying an initial value, it uses the first element of the array as the initial value. This means that the first call to the simplification function uses the first and second array elements as their first and second arguments.

If you call reduce () without an initial value parameter, you will cause the type error exception;

If there is only one value at the time of the call, the array has only one element and no initial value is specified, or an empty array and an initial value is specified. Reduce () simply returns that value without invoking a simplified function.

It is worth noting that the every () and some () methods are a type of array simplification operation. But the difference is that they terminate the traversal as early as possible without always accessing each array element.

Note: Mathematical calculations are not the only intent of reduce () and reduceright (). See an example of the attribute enumeration mentioned earlier

/* Returns a new object that has both the properties of O and the properties of P, and if there is a duplicate attribute in O and P, use the properties in P */

function Union (o,p) {    returnfunction for in p) {    = P[prop];} return o;} var objects = [{X:1},{y:2},{z:3}]; var // =>{x:1,y:2,z:3}

Reduceright works the same way as reduce (), the difference is that it processes arrays from high to low by array index

ECMAScript5 provides 9 new array methods: Traverse, map, filter, detect, simplify, and search arrays

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.