Operations on arrays based on jQuery

Source: Internet
Author: User

JQuery provides two functions:

1. $. map (array, callback (element, index ));

For each element in the array, call the callback () function and return a new array. The original array remains unchanged.

For example:

$ (Function (){
Var arrInt = [1, 3, 5, 79];
Var B = $. map (arrInt, function (value, key, a) {// map returns a new array.
Return value * 2;
});

})

2. $. each (array, fn );

It is mainly used to traverse the array without modifying the array. It can traverse common arrays or "key-value pairs.

You can use this directly in the each function to represent the value of the current element.

For example, retrieve the selected items in the checkbox

<Script type = "text/javascript">

$ (Function (){

Var arr = [];

$ ('Input [type = checkbox] '). click (function (){

$ ('Input [type = checbox]: checked'). each (function (key, value ){

Arr [key] = value. text ();

});

Var lengtha = $ ('input [type = checkbox] [checked = checked] '). length; $ ('# spMsg '). text ('Total' + lengtha + 'is selected:' + arr );

})

})

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.