Differences between the foreach and map and for methods of arrays

Source: Internet
Author: User

First, the definition

foreach (): Iterates through the array, invoking the specified function for each element.

Map (): Passes each element of the called array to the specified function and returns an array that contains the return value of the function.

The passed function is the first parameter of the foreach ()/Map (), which has three parameters: the element of the array + the index of the element + the array itself.

Ii. Summary of differences

1, map speed than foreach faster;

2, MAP returns a new array with the same length as the original array, and does not affect the original array, and foreach does not produce a new array;

3, map because the return is an array so can chain operation, foreach cannot.

recommended use. Map ()

Third, compatibility issues

1. Advanced browsers (including IE9 and above) support map and foreach methods for loop traversal of arrays,IE6~IE8 does not support

Four

Differences between the foreach and map and for methods of arrays

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.