zoukankan      html  css  js  c++  java
  • JavaScript1.6数组新特性和JQuery的几个工具方法



    1、Array.forEach()和jquery的$().each()。在数组中的每个项上运行一个函数。类似java5 增强的for循环

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var ary = [2,4,6,8];
     
    // js1.6 Array.forEach方法
    ary.forEach(function(i){alert(i);});
     
    // jquery的写法
    $(ary).each(function(){alert(this);});
    //还可以写成这样
    $(ary).each(function(index,item){alert(item);});//index是元素的索引,item是该元素

    2、Array.filter()和jquery的$.grep()。在数组中的每个项上运行一个函数,并将函数返回真值的项作为数组返回。简单的说就是用一个条件过滤掉不符合的数组元素,剩下的符合条件的元素组合成新的数组返回。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var ary = [2,4,6,8];
     
    // js1.6 Array.filter()方法
    var otherAry1 = ary.filter(function(item){return item>4;});
    alert(otherAry1);//输出6,8
     
    // jquery写法(注意和$.each的区别)
    // 此处函数中第一个参数是数组元素自身,第二个参数是数组元素索引
    // 而$().each方法刚好相反,作者应该统一下。
    var otherAry2 = $.grep(ary,function(item,index){
         return item>4;
    });
    alert(otherAry2);//输出6,8

    3、Array.map()和jquery的$.map()。在数组中的每个项上运行一个函数,并将全部结果作为数组返回。这个方法非常强大,尤其是作用于DOM数组时(在abcc项目上用过,对每个查询模块DOM生成查询字符串)。简单说就是把每个数组元素运算的结果作为新数组元素(还是很拗口)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var ary = [2,4,6,8];
     
    // js1.6 Array.map()方法
    var newAry1 = ary.map(function(item){return item+1;});//每个元素加1
    alert(newAry1);//输出3,5,7,9
     
    // jquery写法
    var newAry2 = $.map(ary,function(item,index){return item+1;});
    alert(newAry2);//输出3,5,7,9

    4、Array.every()方法。检查数组元素是否都符合某个条件,只要有一个不符合返回false,否则返回true

    1
    2
    3
    4
    var ary = [2,4,6,8,10];
     
    alert(ary.every(function(item){return item>1}));//true
    alert(ary.every(function(item){return item>2}));//false

    5、Array.some()方法。检查数组中元素是否符合某个条件,只要有一个符合返回true,否则返回false

    1
    2
    3
    4
    var ary = [2,4,,6,8,10];
     
    alert(ary.some(function(item){return item>9;}));//true
    alert(ary.some(function(item){return item>10;}));//false

    最后给出 IE6/7/8的解决方案,让这些浏览器完美支持JS1.6的Array新方法。

     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    -function(){
         
    function applyIf(o, c) {
        if(o) {
            for(var in c) {
                if(o[p]===undefined) {
                    o[p] = c[p];
                }
            }
        }
        return o;
    }
    applyIf(Array.prototype, {
        indexOf : function(obj, idx) {
            var from = idx == null ? 0 : (idx < 0 ? Math.max(0, arr.length + idx) : idx);
            for(var i = from, l = this.length; i < l; i++) {
                if(i in this && this[i] === obj) {
                    return i;
                }
            }
            return -1;
        },
        lastIndexOf : function(obj, idx) {
            var len = this.length, from = idx == null ? len - 1 : idx;
            if(from < 0) {
                from = Math.max(0, len + from);
            }
            for(var i = from; i >= 0; i--) {
                if (i in this && this[i] === obj) {
                    return i;
                }
            }
            return -1;
        },
        every : function(fn, thisObj) {
            var l = this.length;
            for(var i = 0; i < l; i++) {
                if(i in this && !fn.call(thisObj, this[i], i, this)) {
                    return false;
                }
            }
            return true;
        },
        some : function(fn, thisObj) {
            var l = this.length;
            for(var i = 0; i < l; i++) {
                if(i in this && fn.call(thisObj, this[i], i, this)) {
                    return true;
                }
            }
            return false;
        },
        filter : function(fn, thisObj) {
            var l = this.length, res = [], resLength = 0;
            for(var i = 0; i < l; i++) {
                if(i in this) {
                    var val = this[i];
                    if(fn.call(thisObj, val, i, this)) {
                        res[resLength++] = val;
                    }
                }
            }
            return res;
        },
        map : function(fn, thisObj) {
            var l = this.length, res = [];
            for(var i = 0; i < l; i++) {
                if(i in this) {
                    res[i] = fn.call(thisObj, this[i], i, this);
                }
            }
            return res;
        },
        forEach : function(fn, thisObj) {
            var l = this.length;
            for(var i = 0; i < l; i++) {
                if(i in this) {
                    fn.call(thisObj, this[i], i, this);
                }
            }
        }
    });
    }();
     
  • 相关阅读:
    matplotlib
    Scipy-数值计算库
    Django Templates
    Django Views: Dynamic Content
    Django Views and URLconfs
    Opencv读写文件
    Logistic回归
    demo
    【Python62--scrapy爬虫框架】
    【Python58--正则2】
  • 原文地址:https://www.cnblogs.com/dqdq/p/4401917.html
Copyright © 2011-2022 走看看