jquery数组处理

简介:

 

 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
  2.    
  3. <html> 
  4.    
  5. <head> 
  6.    
  7.     <title>Hi!</title> 
  8.    
  9.     <script type="text/javascript" src="../scripts/jquery-1.2.1.js"> 
  10.    
  11.     </script> 
  12.    
  13.     <script type="text/javascript"> 
  14.    
  15.       var $ = 'Hi!';  
  16.    
  17.       jQuery(function(){  
  18.    
  19.         alert('$ = '+ $);//这里的 $ 为 Hi!,把它变回jquery的符号:jQuery(function($){}/这样就可以了  
  20.    
  21.    //alert(jQuery)  
  22.    
  23.       });  
  24.    
  25.    jQuery(function($){  
  26.    
  27.    //------------遍历数组 .each的使用-------------  
  28.    
  29.    var anArray = ['one','two','three'];  
  30.    
  31.    $.each(anArray,function(n,value) {  
  32.    
  33.     //do something here  
  34.    
  35.     //alert(n+' '+value);  
  36.    
  37.    });  
  38.    
  39.    var anObject = {one:1, two:2, three:3};  
  40.    
  41.    $.each(anObject,function(name,value) {  
  42.    
  43.     //do something here  
  44.    
  45.     //alert(name+' '+value);  
  46.    
  47.    });  
  48.    
  49.    //-----------过滤数组 .grep的使用------------  
  50.    
  51.    var originalArray =[99,101,103];  
  52.    
  53.    /*//第一种写法  
  54.    
  55.    var bigNumbers = $.grep(originalArray,function(value) {  
  56.    
  57.     return value > 100;  
  58.    
  59.    });  
  60.    
  61.    */  
  62.    
  63.    var bigNumbers = $.grep(originalArray,'a>100');//第2种写法,还可以用正则表达式来过滤  
  64.    
  65.    $.each(bigNumbers,function(n,value) {  
  66.    
  67.     //do something here  
  68.    
  69.     //alert(n+' '+value);  
  70.    
  71.    });  
  72.    
  73.    //------------转换数组 .map的使用------------  
  74.    
  75.    var strings = ['1','2','3','4','S','K','6'];  
  76.    
  77.    var values = $.map(strings,function(value){  
  78.    
  79.     var result = new Number(value);  
  80.    
  81.     return isNaN(result) ? null : result;//如果result不是数字则 返回null(返回null在这里相当于不返回)  
  82.    
  83.    });  
  84.    
  85.    $.each(values,function(n,value) {  
  86.    
  87.     //do something here  
  88.    
  89.     //alert(value);  
  90.    
  91.    });  
  92.    
  93.    var characters = $.map(  
  94.    
  95.     ['this','that','other thing'],  
  96.    
  97.    function(value){return value.split('');}//分离字符串用返回给characters  
  98.    
  99.    );  
  100.    
  101.    //alert(characters.length);  
  102.    
  103.    //------------.inArray(value,array)的使用------------返回value在array下标的位置,如果value不在array中则返回  
  104.    
  105. -1  
  106.    
  107.    var index = $.inArray(2,[1,2,3,4,5]);  
  108.    
  109.    //alert(index);  
  110.    
  111.    //------------makeArray(obj)的使用------------将类数组对象转换为数组对象。  
  112.    
  113.    var arr = jQuery.makeArray(document.getElementsByTagName("div"));  
  114.    
  115.    //arr.reverse(); // 使用数组翻转函数   
  116.    
  117.    $.each(arr,function(n,value) {  
  118.    
  119.     //do something here  
  120.    
  121.     //alert(n+' '+value);  
  122.    
  123.     //alert(value.html());  
  124.    
  125.    });  
  126.    
  127.    var arr2 =$.unique(document.getElementsByTagName("div")); //获得唯一的对象,看API,说得很模  
  128.    
  129. 糊,http://docs.jquery.com/Utilities/jQuery.unique  
  130.    
  131.    alert();  
  132.    
  133.    $.each(arr2,function(n,value) {  
  134.    
  135.     //do something here  
  136.    
  137.     alert(n+' '+value);  
  138.    
  139.    });  
  140.    
  141.    });  
  142.    
  143.     </script> 
  144.    
  145. </head> 
  146.    
  147. <body> 
  148.    
  149. <div>First</div><div>Second</div><div>Third</div><div>Fourth</div><div>Fourth</div> 
  150.    
  151. </body> 
  152.    
  153. </html> 

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1081615


相关文章
|
6月前
|
JavaScript 前端开发 索引
Javascript知识【jQuery:数组遍历和事件】
Javascript知识【jQuery:数组遍历和事件】
|
5月前
|
JavaScript
jQuery判断是否为对象或者数组
jQuery判断是否为对象或者数组
|
3月前
|
JavaScript 小程序 前端开发
jQuery 同时获取多个标签的指定内容并储存为数组
jQuery 同时获取多个标签的指定内容并储存为数组
|
5月前
|
JSON JavaScript 数据格式
jQuery将数组转化成对象
jQuery将数组转化成对象
19 0
|
5月前
|
JavaScript 前端开发
JQuery 获取选中多选框的value,合并成数组传给后台
JQuery 获取选中多选框的value,合并成数组传给后台
22 0
|
5月前
|
JavaScript 前端开发 数据处理
jQuery数据结构渲染(1):图片数组的渲染
jQuery数据结构渲染(1):图片数组的渲染
38 1
|
5月前
|
JSON JavaScript 前端开发
JQuery数组遍历 - $.each(),$().each()和forEach()
JQuery数组遍历 - $.each(),$().each()和forEach()
50 0
|
前端开发 JavaScript
ajax(jquery)前后台传数组(Springmvc后台)
ajax(jquery)前后台传数组(Springmvc后台)
143 0
ajax(jquery)前后台传数组(Springmvc后台)
|
JavaScript
jQuery $.merge()方法合并数组
jQuery $.merge()方法合并数组
102 0
jQuery $.merge()方法合并数组