The foreach, map function in JavaScript

Source: Internet
Author: User

Syntax: both foreach and map support 2 parameters: One is the callback function (Item,index,input) and the context;. foreach: Used to iterate through each item in the array; This method executes without a return value and has no effect on the original array; there are several items in the array, So the anonymous callback function that is passed in will need to be executed several times; each time an anonymous function is executed, it is passed three parameter values: The current item in the array item, the index of the current item, and the original array, input; In theory, this method does not return a value, but simply iterates through each item in the array.    Do not modify the original array, but we can modify the original array by the index of the array; the this default in the anonymous callback function in the ForEach method is window; therefore: [].foreach (function (Value,index,array) { Code something}), equivalent to: $.each ([],function (Index,value,array) {//code something}) will find that the 1th and 2nd arguments are exactly the opposite, and be careful not to remember them correctly. Similar to the following methods, such as $.map. var arr = [1,2,3,4]; var res= arr.foreach (function (Value,index,array) {Array[index] = value*4;}); Console.log (arr); The result is [4,8,12,16]console.log (res); Undefinedmap:map is the meaning of "mapping" similar to the use of ForEach, which is: [].map (function (Value,index,array) {//code}) var ary = [12,23,24,42,1]; var res = Ary.map (function (item,index,input) {return item*10;}) Console.log (res);//-->[120,230,240,420,10]; Console.log (ary);//-->[12,23,24,42,1]?map: Very similar to foreach, which is used to iterate through each of the values in the array, to iterate over each item in the array; difference: The return value is supported in the callback function of map The return is the equivalent of changing the item in the array (without affecting the originalArray, which is just equivalent to cloning a copy of the original array, the clone of this copy of the array of the corresponding item changed);? Whether a foreach or a map supports the second parameter value, the second parameter means to modify this in the anonymous callback function.

foreach, map function

in JavaScript

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.