A summary of jquery array processing methods

Source: Internet
Author: User
Tags array length arrays


Each is used to traverse an array



The each () function is a tool-class function that basically all of the frameworks provide, through which you can iterate through the object, the array's property values, and handle them. Both jquery and jquery objects implement this method, and for jquery objects, simply delegate each method: The jquery object is passed as the first argument to the each method of jquery. In other words The Each method that jquery provides is a method invocation of all of the child elements in the object provided by the argument. The jquery object provides each method to invoke the child elements within jquery.



Instance


The code is as follows

$.each (Array, [callback]) traversal, very common
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



Explanation: Filter The elements of an array using a filter function. This function passes at least two arguments (the third argument is true or false, and the filter function returns a value that is not useful to the individual): an array to filter and a filter function. The filter function must return true to preserve the element or false to delete the element. In addition, the filter function can be set to a note string (individuals do not recommend, to understand their own lookup);


The code is 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])



The map function in jquery helps us to map an array or dataset. The difference between map and filter and grep is that filter and grep change the number of element sets or arrays by filtering, and the map does not change the number of element sets or arrays, only changes the contents.



Datasets use map: Map from an LI element set and modify its specific text, and then add to an element with ID Div2



Arrays use map: Map from the array items and modify their specific text, and then add to the element with ID Div2


The code is 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



The InArray () method is a native indexof () method similar to JavaScript, used to match a number in an array. Return-1 represents, no match found.
If the first element in the array matches the value, $. InArray () returns 0, the subscript of the array value.



If we check that the value exists within the array, we need to check if it is not equal to (or greater than)-1.



Example:


The code is as follows

var m_31 = [1,3,5,7,8,10,12];
var m_30 = [4,6,9,11];
Console.log (Jquery.inarray (3, m_31));
The return value is 1

$.unique (array) filters repeating elements in an array, not used


Blahblahblah .....
$.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



In general, many methods in both jquery and JavaScript return objects that resemble arrays. For example, jquery's proxy function $ () returns a JQuery object with many arrays of properties. (length, [] array access operators, and so on), but arrays are not exactly the same, and some built-in methods for arrays (such as. Pop () and. reverse ()) are missing.


The code is as follows


<! DOCTYPE html>
<style>
div {color:red;}
</style>
<script src= "Jquery-latest.js" ></script>
<body>
<div>First</div>
<div>Second</div>
<div>Third</div>
<div>Fourth</div>
<script>
var elems = document.getelementsbytagname ("div"); Returns a nodelist
var arr = Jquery.makearray (elems);
Arr.reverse (); Use a Array method on the list of DOM elements
$ (arr). Appendto (Document.body);
</script>
</body>



1
var obj = $ (' li ');



var arr = $.makearray (obj);




Result:



(typeof obj = = ' object ' && obj.jquery) = = = true;
Jquery.isarray (arr) = = true;


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.