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

    JavaScript 1.6 引入了几个新的Array 方法,具体的介绍见:New in JavaScript 1.6 。这些方法已经被写进了ECMA262 V5。现代浏览器(IE9/Firefox/Safari/Chrome/Opera)都已经支持,但IE6/7/8不支持。jquery的工具方法中提供了类似的功能。

    1、Array.forEach()和jquery的$().each()。在数组中的每个项上运行一个函数。

    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()。在数组中的每个项上运行一个函数,并将函数返回真值的项作为数组返回。简单的说就是用一个条件过滤掉不符合的数组元素,剩下的符合条件的元素组合成新的数组返回。

    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生成查询字符串)。简单说就是把每个数组元素运算的结果作为新数组元素(还是很拗口)。

    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

    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

    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新方法。

      function applyIf(o, c) {
        if(o) {
            for(var p 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);
                }
            }
        }
    }); 
  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/fengyuqing/p/javascript_new_array.html
Copyright © 2011-2022 走看看