Jquery traverses several methods to Filter Arrays and parse json objects

Source: Internet
Author: User
Tags getscript
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      
       
Related Article

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.