Map in JavaScript, ForEach

Source: Internet
Author: User

. Map (FN) method

1 var a=[2, 4, 6];2 a.map (function (d, I, arr) {3 return {x:i, y:d*i, z:arr};4});

Output Result:

1 [{x:0, y:0, Z:[2, 4, 6]}, 2 {x:1, Y:4, Z:[2, 4, 6]}, 3 {x:2, Y:12, Z:[2, 4, 6]}

The d in the callback function is the value of the traversal array, and I is the index of the array, and the entire array is passed in the third parameter.

Map casts the original array into a new array of the same length, and the value of each corresponding member in the new array is the value returned by the callback function.

The. ForEach (FN) method

1 var arr=[1, 3, 5];2 Arr.foreach (function (d, I, arr) {3 Console.log ([I, D, arr]); 4});

The results are as follows:

[0, 1, [1, 3, 5]] [0, 3, [1, 3, 5]] [0, 5, [1, 3, 5]]

Traverse arr, each pass in the values D and index I and the entire arr

Map in JavaScript, ForEach

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.