posts - 93,  comments - 2,  trackbacks - 0
1.利用数组在列表中显示名字
    (1)纵向显示
    如:<script type="text/javascript>
                $(document).ready(function(){
                        var member = ["John","Kate","Peter"];
                        $('p').html(member.join('<br />'));
                }
        </script>
    (2)显示名字及其在数组中所在位置
    如:<script type="text/javascript>
            $(document).ready(function(){
            var member = ["John","Kate","Peter"];
            $.each(member,function(n,value){
            $('ol').append($('<li>'+value+'</li>'));
            });
        });
        </script>
     (3)利用Html元素创建数组及计数数组的长度
        $(document).ready(function(){

       Var names=$(‘li’).get();    //创建数组

       $(‘p’).text(names.length);     //计数数组长度
});

2.操作数组数据(转换成大写)
  $(document).ready(function(){ 
        var member=["zhangsan","BoLaiTe"];

       var member=$.map(member,function(n,i){    //member可以变为其他数组名
            return (i+1+"."+n.toUpperCase());
        });

});
注:$.each()和$.map()都是迭代数组中的每个元素,并调用回调函数对每个元素做操作。后者还能返回一个数组元素。

3.筛选数组元素
    (1)使用grep
    $(document).ready(function(){
        member=["Dev","Tom"];
        member=$.grep(member,function(v){return v.length>3});
    });
    (2)使用match
        member=["Dev","Tom"];
        member=$.grep(member,function(v){return v.match(/^[A-D]/)});
4.字符串数组,数值数组排序
    (1)字符串数组排序
           var member=["dev","Tom"]
            member=member.sort();
      (2) 数值数组排序
           var memer=[12,34,13,53];
            member=member.sort(function(a,b){
                return a-b;
            });
      (3)对象数组排序
            <script type="text/javascript" src="rs/js/jquery.js"></script>

                <script type="text/javascript">

                <!--

                $(

                    function()

                    {

                        var students =[

                        {'sid':'ST001','sname':'张三','sage':18},

                        {'sid':'ST004','sname':'赵六','sage':23},

                        {'sid':'ST002','sname':'李四','sage':42},

                        {'sid':'ST003','sname':'王五','sage':35}

                    ];

                    //表格显示

                   $.each(students,

                          function(index, value)

                          {

                                $('#ia').append('<tr><td>' + value.sid +

                                '</td><td>' + value.sname +

                                '</td><td>' + value.sage + '</td></tr>');

                          }

                    );

                    //按照SID排序

                    var sidOrder = students.sort(

                        function(a, b)

                        {

                        if(a.sid < b.sid) return -1;

                        if(a.sid > b.sid) return 1;

                        return 0;

                    }

                    );

                    $.each(sidOrder,

                    function(index, value)

                    {

                        $('#ib').append('<tr><td>' + value.sid +

                            '</td><td>' + value.sname +

                            '</td><td>' + value.sage + '</td></tr>')

                        }

                    );

                    //按照SAGE排序

                    var sageOrder = students.sort(

                        function(a, b)

                        {

                            return (a.sage - b.sage);

                        }

                    );

                    $.each(sageOrder,

                        function(index, value)

                        {

                            $('#ic').append('<tr><td>' + value.sid +

                            '</td><td>' + value.sname +

                            '</td><td>' + value.sage + '</td></tr>')

                        }

                    );

                }

            );

            //-->

    </script>

注:上述来源于:http://mmz06.blog.163.com/blog/static/12141696201102935021258/
5.拆分数组
    var member=[20,40,21,302,21];
    var memSecond=member.splice(1,2);
    得到 member=[20,302,21];
            memSecond=[40,21];
6.合并数组
    var mem1=[1,2];
    var mem2=[3,4];
    var mem=mem1.concat(mem2);
             
            

posted on 2011-07-21 20:47 Terry Zou 阅读(1270) 评论(0)  编辑  收藏 所属分类: jQuery

只有注册用户登录后才能发表评论。


网站导航:
 
<2011年7月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜