Bobo jquery filter Array grep, each, InArray, map usage and traverse JSON object [go]

Source: Internet
Author: User

There are several uses for purely recording:

jquery grep () filter Traversal array

$ (). Ready (   function () {       var array = [1,2,3,4,5,6,7,8,9];       var filterarray = $.grep (array,function (value) {           return value > 5;//filter out greater than 5       });       for (Var i=0;i<filterarray.length;i++) {           alert (filterarray[i]);       }       For (key in Filterarray) {           alert (Filterarray[key]);       }   });

jquery each() Filter Traversal array

$ (). Ready (   function () {       var anobject = {one:1,two:2,three:3};//to the JSON array each       $.each (anobject,function ( Name,value) {           alert (name);           alert (value);       });       var anarray = [' One ', ' one ', ' three '];       $.each (Anarray,function (n,value) {           alert (n);           alert (value);       });   

jquery inArray() Filter Traversal array

$ (). Ready (   function () {       var anarray = [' One ', ' one ', ' three ');       var index = $.inarray (' Both ', anarray);       alert (index);//Returns the value in the array of key values, returns 1       alert (Anarray[index]);//value is the "   );

jquery Map () filter Traversal array

$ (). 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]);       }   });

jquery Traversal parsing JSON object 1:

var json = [{dd: ' SB ', AA: ' Dongdong ', RE1:123},{CCCC: ' DD ', LK: ' 1qw '}];for (var i=0,l=json.length;i<l;i++) {for   (var key In Json[i]) {       alert (key+ ': ' +json[i][key]);}   }

jquery Traversal parsing JSON object 2

There is the following JSON object: Var obj ={"name": "Feng Juan", "Password": "123456″," "Department": "Technical department", "Sex": "female", "old": 30};//Traversal Method: for (Var p in obj) {   str = str+obj[p]+ ', ';   return str;}

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.