Two functions in jquery: Map and each

Source: Internet
Author: User

The first is map,

Syntax: $.map (ARR,FUNC);//functin (Ele,i)

Note that the rollback function is a parameter, and the index and value of the array object that represents the loop

The goal is to turn the process operation into a functional operation

Function: Use the back function to process elements in each array or object and return a new one

For example, double the number of values in the array that are greater than 3

  var arr = [One,
        A, a, a, a, a, a, a. var New_arr = $.map (arr, function (V, i) {
            return  v * (i% 2 = = 0? 1:2);

        });
        alert (New_arr);

followed by each loop:

Syntax: $.each (COLLECTION,FUNC);//function (K,V)

Note that the parameters of the callback function are the keys and values of each member

The function is to iterate over an object or an array, noting that nothing is returned

If you need to stop the traversal, use return false;

Example:

var o = {name: "Xiaoming", age:19, Sex: "Male"};
        $.each (o, function (k, v) {
            Console.log (k + "--" + V);
        });
The basic idea of the map loop and each loop is to turn the procedure-oriented operation into a functional operation. Convenient for chained programming (functional programming)

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.