Jquerygrep () filtering and traversing arrays $ () ready (function () {vararray [, 9]; varfilterarray $ grep (array, function (value ){
Jquery grep () filters and traverses Arrays
$ (). Ready (
Function (){
Var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var filterarray = $. grep (array, function (value ){
Return value> 5; // filter out values greater than 5
});
For (var I = 0; I Alert (filterarray [I]);
}
For (key in filterarray ){
Alert (filterarray [key]);
}
}
);
Jquery each () filters and traverses Arrays
$ (). Ready (
Function (){
Var anObject = {one: 1, two: 2, three: 3}; // For the json array each
$. Each (anObject, function (name, value ){
Alert (name );
Alert (value );
});
Var anArray = ['one', 'two', 'three '];
$. Each (anArray, function (n, value ){
Alert (n );
Alert (value );
}
);
}
);
Jquery inArray () filter and traverse Arrays
$ (). Ready (
Function (){
Var anArray = ['one', 'two', 'three '];
Var index = $. inArray ('two', anArray );
Alert (index); // returns the key value of this value in the array, and returns 1
Alert (anArray [index]); // value is two
}
);
Jquery map () filter and traverse Arrays
$ (). Ready (
Function (){
Var strings = ['0', '1', '2', '3', '4', 's', '6'];
Var values = $. map (strings, function (value ){
Var result = new Number (value );
Return isNaN (result )? Null: result; // isNaN: is Not a Number abbreviation
}
);
For (key in values ){
Alert (values [key]);
}
}
);
Parse json object 1 through js Traversal
Var json = [{dd: 'SB ', AA: 'dongdong', re1: 123}, {cccc: 'dd', lk: '1qw'}];
For (var I = 0, l = json. length; I For (var key in json [I]) {
Alert (key + ':' + json [I] [key]);
}
}
Parse json object 2 through js Traversal
The following json objects are available:
Var obj = {"name": "Feng Juan", "password": "123456", "department": "technology department", "sex": "female", "old ": 30 };
Traversal method:
For (var p in obj ){
Str = str + obj [p] + ',';
Return str;
}
The following example shows the specific implementation method.
JQuery method of retrieving objects $ ('# id'): using the element id $ ('tagname'): using the element Tag Name $ ('tagname tagname '): use the element's tag name, eg: $ ('ul li') $ ('tagname # id): use the element's id and tag signature $ (': checkbox '): take all elements of the input type as checkbox: Eg:$ ('Span [price] input [type = text] '): retrieves the following input element.$ ('Div ', $ (this ). parents ('div: first '): gets the first div node on the div (at least the parent node) $ ('~ Span: first ', this): locates the first sibling of this that's a element. Delays loading js files: $. getScript example: Html file: Java code $. GetScript Example