JavaScript's map loop, foreach Loop, filter loop

Source: Internet
Author: User

1. Map loop

Let Arr=[1,2,3,4];arr.map (function (Value,key,arr) {//value, index, array (default = selected array)    return item; Returns an array of all undefined if no return, number is index +1 (key+1)})//returns an array

2. Foreach Loop

Let Arr=[1,2,3,4];arr.map (function (Value,key,arr) {//value, index, array (default = selected array)    return item; Do anything})//return only undefined

3. Filter loop

Let Arr=[1,2,3,4];arr.map (function (Value,key,arr) {//value, index, array (default = selected array)    return false; Based on true and false to see if the original array is returned})//false return [],true returns only the original array

4. Reduce cycle (from beginning to end)

Let Arr=[1,2,3,4];arr.map (function (Fristvalue,nextvalue,key,arr) {//Array first value (first occurrence), next value of array, index, array (default is selected array)    return fristvalue+nextvalue; Add all values, always iterate})//Return the result of the return iteration

5. Reduceright cycle (from tail to head)

Let Arr=[1,2,3,4];arr.map (function (Fristvalue,nextvalue,key,arr) {//array last value (first occurrence), array next value, index, array (default = selected array)    return fristvalue+nextvalue; Add all values, always iterate})//Return the result of the return iteration

  

JavaScript's map loop, foreach Loop, filter loop

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.