Jquery Array Operations

Source: Internet
Author: User

Jquery Array Common operations

Reference Blog: http://www.jb51.net/article/43164.htm

1. $.each (array, [callback]) traversal [common]

Explain:

Unlike the $ (). each () method that iterates over a jquery object, this method can be used for any object (not just an array).

The callback function has two parameters: the first is the index of the object's member or array, and the second is the corresponding variable or content.

If you need to exit each loop so that the callback function returns false, the other return values are ignored.

Each traversal, in the usual event handling, is a variant of the For loop, but is more powerful than a for loop. In the array, it can easily take the array index and the corresponding value. Example:

var_mozi=['Mohist School','Mozi','Modi','and love non-offensive','Shang Tong Shangxian'];//the array used in this article, the same as$.each (_mozi,function (key,val) {//The callback function has two parameters, the first is an element index, the second is the current valueAlert'_mozi Array, index:'+key+'the corresponding values are:'+val);}); 

For: relative to the native. In,each a little stronger. For: In can also iterate through the array and return the corresponding index, but the value needs to be obtained by Arrname[key]

2. $.grep (Array, callback, [invert]) filter array [Common]

Explanation: Use the filter function to filter the array elements. This function passes at least two arguments (the third argument is true or false, and the return value of the filter function is reversed): Array to filter and filter function. The filter function must return true to preserve the element or false to delete the element. In addition, the filter function can also be set to a string;

var arr = [12345]; var 2 ;   =return value! = RemoveItem;});

3. $.inarray (Val,array) determine if the value exists in the array [common]

Explanation: Determines the position of the first parameter in the array, counting from 0 (returns 1 if not found)
IndexOf () returns the first occurrence of the string, and $.inarray () returns the position of the passed-in parameter in the array, similarly, if found, returns a value greater than or equal to 0, or 1 if not found. Use it to determine whether a value exists in the array

var_exist=$.inarray ('Mozi', _mozi);var_inexistence=$.inarray ('Wei', _mozi)if(_exist>=0) {alert ('Mozi exists in the array _mozi, and its index value in the array is:'+_exist);}if(_inexistence<0) {alert ('Wei does not exist in the array _mozi!, the return value is:'+_inexistence+'!');} 

Not finished ...

Jquery Array Operations

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.