Jquery operates on Arrays

Source: Internet
Author: User

Jquery provides two functions:

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

For each element in the array, call the callback () function to 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 );




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.