Use grep, each, inarray, and map of jquery to Filter Arrays and traverse JSON objects

Source: Internet
Author: User

The following usage is recorded:

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 more than 5  });  For (VaR I = 0; I <filterarray. length; I ++ ) {Alert (filterarray [I]);}  For (Key In  Filterarray) {alert (filterarray [Key]) ;}}); 

JqueryEach()Filter and traverse Arrays

$ (). Ready (Function(){VaRAnobject = {one: 1, two: 2, three: 3 };//For the JSON array each$. Each (anobject,Function(Name, value) {alert (name); alert (value );});VaRAnarray = ['one', 'two', 'three']; $. Each (anarray,Function(N, value) {alert (n); alert (value );});});

JqueryInarray()Filter and traverse Arrays

$ (). Ready (Function(){VaRAnarray = ['one', 'two', 'three'];VaRIndex =$. Inarray ('two', anarray); alert (INDEX );//Returns the key value of this value in the array, and returns 1Alert (anarray [Index]);//Value is two});

JqueryMap () filters and traverses 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]) ;}}); 

JqueryParse JSON object 1:

 
VaRJSON = [{DD: 'SB ', AA: 'dongdong', RE1: 123}, {CCCC: 'dd', lk: '1qw'}];For(VaRI = 0, L = JSON. length; I <L; I ++){For(VaRKeyInJSON [I]) {alert (Key+ ':'+ JSON [I] [Key]) ;}}

Jquery traverses and parses JSON object 2

// The following JSON object is available:VaROBJ = {"name": "Feng Juan", "Password": "123456", "department": "technology department", "sex": "female", "old": 30}; // Traversal method:For(VaRPInOBJ) {Str= STR + OBJ [p] +',';ReturnSTR ;}

 

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.