Several methods of the jquery traversal filter array

Source: Internet
Author: User
Tags button type getscript json split tag name tagname

  This article introduces several methods of the jquery traversal filtering array and the concrete implementation of parsing the JSON object, and interested friends don't miss the

jquery grep () filter traversal array     code as follows: $ (). Ready (  function () {  var array = [1,2,3,4,5,6,7,8,9];  var filt Erarray = $.grep (array,function (value) {  return value > 5;//filter for  } with 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   Code as follows: $ (). Ready (  function () {  var anobject = { one:1,two:2,three:3};//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); nbsp Alert (value); } ); } );    jquery InArray () filter traversal array   Code as follows: $ (). Ready (  function () {  var anarray = [' One ', ' two ', ' three '];  var index = $.inarray (' two ', Anarray);  alert (index); /returns the key value in the array, returns 1  alert (Anarray[index]);//value is TWo } );    jquery map () filter traversal array   Code as follows: $ (). 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)); }&nbsp ; } );    JS Traversal parse the JSON object 1    code as follows: var json = [{dd: ' SB ', AA: ' Dongdong ', RE1:123},{CCCC: ' DD ', LK: ' 1qw '}];& nbsp for (Var i=0,l=json.length;i<l;i++) {  to (Var key in json[i)) {  alert (key+ ': ' +json[i][key]); }  }    JS traversal parsing JSON objects 2    has the following JSON object:  var obj ={"name": "Feng Juan", "Password": "123456″," Department ": Technical department", "Sex": "female", "old":30};  Traversal method:    code as follows: for (var p in obj) {  str = str+obj[p]+ ', '; nbsp Return str; }    Below is an example of how to implement     jquery fetching objects     $ (' #id '): Through the elements of the id  $ (' TagName '): Through the element's label name   $ (' tAgname TagName '): Through the label name of the element, eg: $ (' ul Li ')   $ (' Tagname#id): Through the element ID and tag name   $ (': CheckBox '): Take the input All elements of type checkbox ':  Eg: <input type= "checkbox" Name= "appetizers"   value= "Imperial"/>    $ (' Span[price] input[type=text]: Take the following INPUT element   <span price= "3" >  <input type= "text" Name= " Imperial.quantity "  disabled=" Disabled "value=" 1 "/>  </span>  $ (' div ', $ (this). Parents (' div : First '): Take the div node   $ (' ~ Span:first ', this) on the top (or at least the parent node) of it: Locates the "a" and "s", <span&gt ; element.    Delay Loading JS file:  $.getscript    Example:  HTML file:    code as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >  <html>  <head>  <title>$.getscript example</title>  <link rel= "stylesheet" type= "Text/css" Href= ". /common.css ">  <script type=" text/javascript "  src=". /scripts/jquery-1.2.1.js "></script>  <script type=" Text/javascript ">  $ (function () {  $ (' #loadButton ' . Click (function () {  $.getscript (//) An error occurs in firefox/3.0.1 (syntax error, defined variable does not work, ff2 no problem)   ' new.stuff.js '//, Function () {$ (' #inspectButton '). Click ()} ); });  $ (' #inspectButton '). Click (function () {  SomeFunction (somevariable);  test ()  }); };  </script>  </head>    <body>  <button type= "button" id= "Loadbutton" >Load</button>  <button type= "button" ID = "Inspectbutton" >Inspect</button>  </body>  </html>  <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >  <html>  <head>  <title>$.getscript example</title>  <link rel= "stylesheet" type= "Text/css" Href= ". /common.css ">  <script type=" text/javascript "  src=". /scripts/jquery-1.2.1.js "></script>  <script type= "Text/javascript" >  $ (function () {  $ (' #loadButton '). Click (Function ( {  $.getscript (//) An error occurs in firefox/3.0.1 (syntax error, defined variable does not work, ff2 no problem)   ' new.stuff.js '//,function () {$ (' # Inspectbutton '). Click ()} ); });  $ (' #inspectButton '). Click (function () {  someFunction ( somevariable);  Test ()  }); });  </script>  </head>  <body>  <button type= "button" id= "Loadbutton" >Load</button>  <button type= "button" id= "Inspectbutton" >Inspect</button>  </body>  </html>    JS file:    Code as follows: Alert ("I ' m Inline! ");     var somevariable = ' Value of somevariable ';    function someFunction (value) {  alert (value); nbsp }    function test () {  alert (' Test '); }  alert ("I ' m inline!");   var somevariable = ' Value of somevariable ';  function someFunction (value) {  alert (VALue); }  function test () {  alert (' Test '); }    jquery array processing:    code is as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >  <html>  <head>  <title>Hi!</title>  <script type= "Text/javascript" src= ". /scripts/jquery-1.2.1.js ">  </script>  <script type=" Text/javascript ">  var $ = ' hi! ';   jquery (function () {  alert (' $ = ' + $);//Here's $ hi!, turn it back to jquery: jquery (function ($) {...} /This is OK  //alert (jquery)    });  JQuery (function ($) { //------------traversal array. Each use-------------  var anarray = [' One ', ' two ', ' Three '];  $.each (anarray,function (n,value) { //do Something here //alert (n+ ' +value); });  var anobject = {one:1, two:2, three:3};  $.each (AnObject, function (name,value) { //do something here //alert (name+ ' +value); });   //-----------filter numberGroup. grep usage------------  var originalarray =[99,101,103];    var bignumbers = $.grep (Originalarray, ' a> 100 '); the 2nd way of writing, you can also use regular expressions to filter   $.each (bignumbers,function (n,value) { //do something here //alert (n+ ' + Value); });   //------------convert array. Use of map------------  var strings = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' S ', ' K ', ' 6 ' ];  var values = $.map (strings,function (value) {  var result = new Number (value);  return to isNaN (result)? nu ll:result;//returns NULL if result is not a number (returns null here equivalent to not returning)  });  $.each (values,function (n,value) { //do Something here //alert (value); });    var characters = $.map (  [' This ', ' so ', ' other thing '],   function (value) {return value.split (');} Detach string with return to characters ); //alert (characters.length);   //------------. InArray (Value,array) Uses------------returns the position of value at the bottom of the array, and returns -1  var index = $.inarray (2,[1,2,3,4,5]); //alert (if value is not in the array) ( Index);  &nbsP ------------Makearray (obj) uses------------converts an array object of a class to a group object.   var arr = Jquery.makearray (Document.getelementsbytagname_r ("div")); //arr.reverse (); Use array rollover functions   $.each (Arr,function (n,value) { //do something here //alert (n+ ' ' +value); //alert ( Value.html ()); });  var arr2 =$.unique ("div"); Get a unique object, look at the API, speak very   paste,http://docs.jquery.com/utilities/jquery.unique  alert ();  $.each (ARR2, function (n,value) { //do something here  alert (n+ ' +value); }); });  </script>  </head>  <body>  <div>first</div><div>second</div><div>third </div><div>Fourth</div><div>Fourth</div>  </body>  </html>   <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//en" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" >  <html>  <head>  <title>Hi!</title>  <script type= "Text/javascript" src= ". /scripts/jquery-1.2.1.js ">  </script>  <script type=" Text/javascript ">  var $ = ' hi! ';   jquery (function () {  alert (' $ = ' + $);//Here's $ hi!, turn it back to jquery: jquery (function ($) {...} /This is OK  //alert (jquery)  });  JQuery (function ($) { //------------traversal array. Use of each-------------   var anarray = [' One ', ' two ', ' Three '];  $.each (anarray,function (n,value) { //do-something here // Alert (n+ ' +value); });  var anobject = {one:1, two:2, three:3};  $.each (anobject,function (name,value) {  //do something here //alert (name+ ' +value); }); //-----------filter array. Use------------of grep   var originalarray =[99,101,103];    var bignumbers = $.grep (Originalarray, ' a>100 ');//2nd way, you can also use regular expressions to filter & nbsp $.each (Bignumbers,function (n,value) { //do something here //alert (n+ ' ' +value); }); //-------- ----Convert array. Use of map------------&NBSp var strings = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' S ', ' K ', ' 6 '];  var values = $.map (strings,function (value) {  var result = new Numb ER (value);  return isNaN (result)? null:result;//returns NULL if result is not a number (returns null here equivalent to not returning)  });  $.each (values,function (n,value) { //do Something here //alert (value); });  var characters = $.map (  [' This ', ' so ', ' other thing '],  function (value) {return value.split (');} Detach string with return to characters ); //alert (characters.length); //------------. InArray (Value,array) Use------------returns the position of value in the array subscript, if value is not in array, returns   -1  var index = $.inarray (2,[1,2,3,4,5]); // The use of alert (index); //------------Makearray (obj)------------converts an array object of a class to a group object.   var arr = Jquery.makearray (Document.getelementsbytagname_r ("div")); //arr.reverse (); Use array rollover functions   $.each (Arr,function (n,value) { //do something here //alert (n+ ' ' +value); //alert ( Value.html ()); });  var arr2 =$.uniqUE (Document.getelementsbytagname_r ("div")); Get a unique object, look at the API, speak very   paste,http://docs.jquery.com/utilities/jquery.unique  alert ();  $.each (ARR2, function (n,value) { //do something here  alert (n+ ' +value); }); });  </script>  </head>  <body>  <div>first</div><div>second</div><div>third </div><div>Fourth</div><div>Fourth</div>  </body>  </html>      
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.