Several methods of jquery traversal filtering arrays and traversal parsing JSON objects | Map () method detailed

Source: Internet
Author: User

Several methods of jquery traversal filtering arrays and traversal parsing JSON objects | Map () method detailed

One, jquery traversal filter Array

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


2, 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);       });   


3. 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 "   );


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


Second, traverse parsing JSON object

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


2. jquery traversal parsing JSON object 2
The following JSON objects are available:
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;}


Three, Map () method detailed
1. Example

To build a list of all values in the form:

$ ("P"). Append ($ ("input"). Map (function () {  return $ (this). val ();}). Get (). Join (","));


2. Definition and usage

Map () passes each element through a function to the current matching collection, generating a new JQuery object that contains the return value.

3 syntax

. Map (Callback (index,domelement))

Parameters


Describe

Callback (Index,domelement)


The function object that is called for each element in the current collection.

Detailed description

Because the return value is an array of jQuery encapsulation, use get () to process the returned object to get the underlying array.

The. Map () method is particularly useful for getting or setting the value of an element set. Consider the following form with a series of check boxes:

<form method= "POST" action= "" > <fieldset>    <div>      <label for= "both" >2</label>      <input type= "checkbox" value= "2" id= "and" Name= "number[" >    </div>    <div>      < Label for= "Four" >4</label>      <input type= "checkboxes" value= "4" id= "four" name= "number[" ">    </ div>    <div>      <label for= "Six" >6</label>      <input type= "checkbox" value= "6" id= " Six "name=" number[">    </div>    <div>      <label for=" eight ">8</label>      <input type= "checkbox" value= "8" id= "eight" name= "number[" ">    </div> </fieldset></form>


We are able to get a comma-delimited list of check box IDs:

$ (': CheckBox '). Map (function () {  return this.id;}). Get (). Join (', ');


The result of this call is the string: "Two,four,six,eight".
Inside the callback function, this references the current DOM element for each iteration. The function can return a separate data item, or an array of data items to be inserted into the result set. If an array is returned, the elements within the array are inserted into the collection. If the function returns null or undefined, no element is inserted.

Several methods of jquery traversal filtering arrays and traversal parsing JSON objects | Map () method detailed

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.