JQuery Array Processing (including instance Demonstration), jquery Array

Source: Internet
Author: User

JQuery Array Processing (including instance Demonstration), jquery Array
Array Used for demonstration

Var _ mozi = ['mojie', 'mozi', 'mozi', 'both love and non-attacking ', 'shang Tong Shang Xian'];

1. $. each traversal example [common]

$. Each (_ mozi, function (key, val) {// The callback function has two parameters. The first is the element index, and the second is the current value in the alert ('_ mozi array, index: '+ key +' corresponds to '+ val );});

2. $. grep () Filter Arrays [common]

$. Grep (_ mozi, function (val, key) {// The filter function has two parameters: the first is the current element, and the second is the element index if (val = 'mozi ') {alert (the subscript of 'array value is: '+ key) ;}}); var _ moziGt1 = $. grep (_ mozi, function (val, key) {return key> 1 ;}); alert (the element whose index value is greater than 1 in the '_ mozi array is: '+ _ moziGt1); var _ moziLt1 = $. grep (_ mozi, function (val, key) {return key> 1 ;}, true); // The third reliable parameter is input here, return values in the filter function using the reverse alert (the element with the index value less than or equal to 1 in the '_ mozi array is' + _ moziLt1 );

3. $. map () converts arrays by given conditions [general]

Var _ mapArrA = $. map (_ mozi, function (val) {return val + '[new addition]';}); var _ mapArrB = $. map (_ mozi, function (val) {return val = 'mozi '? '[Add only to Mozi]' + val: val;}); var _ mapArrC = $. map (_ mozi, function (val) {// expands a new element return [val, (val + '[extension]')] For the array element;}); alert ('the array after each element is appended with the \ '[new addition] \' character is: '+ _ mapArrA ); alert ('the array after only adding characters to the element Mozi is '+ _ mapArrB); alert (' is each element in the original array, expand an element that adds the character \ '[new] \', and the returned array is '+ _ mapArrC );

4. $. inArray () determines whether the value exists in the array [common]

Var _ exist = $. inArray ('mozi ', _ mozi); var _ inexistence = $. inArray ('wei Yang ', _ mozi) if (_ exist> = 0) {alert ('mozi exists in array _ mozi, and its index value in the array is: '+ _ exist);} if (_ inexistence <0) {alert ('wei Yang does not exist in array _ mozi !, Returned value: '+ _ inexistence + '! ');}

5. $. merge () merge two arrays [general]

// Native concat () may be more concise than it _ moziNew = $. merge (_ mozi, ['guimigu ', 'shangyang', 'sunning', 'ponjuan ', 'suqin', 'zhangyi ']) alert ('the length of the new array after merging is '+ _ moziNew. length + '. the value is '+ _ moziNew );

6. $. unique () filters repeated elements in the array [not commonly used]

Var _ h2Arr = $. makeArray (h2obj); // repeat array _ h2Arr once _ h2Arr = $. merge (_ h2Arr, _ h2Arr); var _ curLen = _ h2Arr. length; _ h2Arr = $. unique (_ h2Arr); var _ newLen = _ h2Arr. length; alert (the original length value of 'array _ h2Arr is '+ _ curLen +', which is '+ _ newLen +' After filtering '. filter '+ (_ curLen-_ newLen) + 'repeated elements ')

7. $. makeArray () Class array object conversion to array [not commonly used]

Var _ makeArr = $. makeArray (h2obj); alert ('convert the Data Type of the h2 element object set to '+ _ makeArr. constructor. name );

8. $. toArray () Restore all DOM elements to an array [not commonly used]

Var _ toArr = $('h2 '). toArray (); alert (the data type after 'h2 Element Set restoration is' + _ toArr. constructor. name );

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.