Several iterative methods and merging methods of arrays 1

Source: Internet
Author: User

Iterative methods:
Every () runs the given function for each item in the array, and returns True if the function returns true for each item. Use this method to determine whether all items in the array meet a condition, similar to the meaning of &&.
var arr = [1,2,3,4,5,6,7,8];

var everyres = arr.every (function (Item,index,array) {

return (ITEM>2);

});
alert (everyres);
Some () runs the given function for each item in the array, and returns True if the function returns true for either item. Use this method to determine whether all the items in the array meet the criteria. Similar to | | The meaning.

var someres = arr.some (function (item,index,array) {

return (ITEM>2);

});

alert (someres);
Filter () Executes a test function on each item in the array and constructs a new one, and returns a true item to be added to the new array. If an item returns false, the item is not included in the new array
var filterres = arr.filter (function (item,index,array) {

return (ITEM>2);

});

alert (filterres);

The map () runs the given function for each item in the array, returning a list of the results of each function call.
var mapres = Arr.map (function (item,index,array) {

return (ITEM*2);

});

alert (mapres);

ForEach () has no return value.
ForEach () {}
Merge method:
Reduce () accepts the incoming function and the initial value (optional) that is the basis for the merge.

The parameter to pass in contains four parameters, the previous value, the current value, the index of the item, and the array object (the first occurrence of the first iteration is the first parameter when it occurs in the second item).

var arr = [1,2,3,4,5];
var sum = arr.reduce (function (Prev,cur,index,array) {

return prev + cur;

});

alert (sum); 15

The Reduceright () and the reduce () methods are essentially the same, except that they are traversed from the back forward.

var sum = arr.reduce (function (prev,cur,index,array) {

return prev + cur;

});

alert (sum); 15

Several iterative methods and merging methods of arrays 1

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.