jquery Array Processing method Rollup _jquery

Source: Internet
Author: User
Tags array length arrays
$.each (Array, [callback]) traversal, very common
Copy Code code as follows:

var arr = [' JavaScript ', ' php ', ' Java ', ' C + + ', ' C # ', ' Perl ', ' VB ', ' HTML ', ' CSS ', ' objective-c '];
$.each (arr, function (key, Val) {
Firebug Console
Console.log (' Index in arr: ' + key + ", corresponding value:" + val);
If you want to exit the loop
return false;
});

$.grep (Array, callback, [invert]) filter, commonly used
Copy Code code as follows:

var temp = [];
temp = $.grep (arr, function (val, key) {
if (Val.indexof (' C ')!=-1)
return true;
If the [invert] parameter is not given or false, $.grep collects only the array elements that return true for the callback function
If the [invert] argument is true, the array element that collects the callback function returns false $.grep
}, False);
Console.dir (temp);

$.map (Array, [callback]) not used too much
Copy Code code as follows:

Versions prior to 1.6 only support arrays
temp = $.map (arr, function (val, key) {
Returns NULL, the array length returned is minus 1
if (val = = ' vb ') return null;
return Val;
});
Console.dir (temp);
1.6 Starting to support JSON-formatted object
var obj = {key1: ' Val1 ', Key2: ' Val2 ', Key3: ' Val3 '};
temp = $.map (obj, function (val, key) {
return Val;
});
Console.dir (temp);

$.inarray (val, array) determines whether the specified array is used in the
Copy Code code as follows:

Returns the position of the element in the array, 0 is the starting position, and return-1 does not find the element
Console.log ($.inarray (' JavaScript ', arr));

$.merge (first, second) merges two arrays, using frequencies generally
Copy Code code as follows:

var frontend = [' JavaScript ', ' CSS ', ' html '],
backend = [' java ', ' php ', ' C + + '];
This way modifies the first argument, the frontend array
temp = $.merge (frontend, backend);
Console.dir (temp);
Console.dir (frontend);
You can use the following methods to avoid the effect on the original array
$.merge ($.merge ([], frontend), backend);

$.unique (array) filters repeating elements in an array, not used
Copy Code code as follows:

<DIV>blahblahblah....</DIV>
<DIV></DIV>
<div class=dup></div>
<div class=dup></div>
<div class=dup></div>
<DIV></DIV>
$.unique supports only DOM element arrays, eliminates duplicate DOM elements, and does not support other types of arrays (string or number)
Get the original Dom array, not the jquery encapsulated
var divs = $ (' div '). get ();
Add a few class div for DUP
DIVs = Divs.concat ($ (' div.dup '). get ());
Console.log ("before unique:" + divs.length);
DIVs = $.unique (divs);
Console.log ("After unique:" + divs.length);

$.makearray (obj) converts class array objects into groups, infrequently
Copy Code code as follows:

First, what is a class array object? The jquery official web site uses divs = Getelementsbytag (' div ') for example
This divs has some sort of array-like length, get the element through [index], etc.
Then using $.makearray (divs) to make it into an array, you can use the other features of the array
such as reverse (), pop (), etc.

$ (DOM). ToArray () Restores the jquery collection to an array of DOM, not used
Copy Code code as follows:


Like Makearray, quite infrequently, the general situation can be ignored
This article refers to Mr.think's blog, here thanks for sharing

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.