打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
js 数组ES5新方法的兼容处理

  1. forEach (js v1.6)
  2. map (js v1.6)
  3. filter (js v1.6)
  4. some (js v1.6)
  5. every (js v1.6)
  6. indexOf (js v1.6)
  7. lastIndexOf (js v1.6)
  8. reduce (js v1.8)
  9. reduceRight (js v1.8)

 
    // 最基础的forEach
    function forEach(array, action) {
        for (var i = 0; i < array.length; i++) {
            action(array[i]);
        }
    }
 
    // 测试forEach
    forEach(["Pear", "Apple"], function(name) {
        console.log(name);
    });
 
    // ------------------------------------------------ //
    // 实现reduce 简化版
    function reduce(combine, base, array) {
        forEach(array, function(element) {
            base = combine(base, element);
        });
        return base;
    }
  //兼容版
function(callback, opt_initialValue){ 'use strict'; if (null === this || 'undefined' === typeof this) { // At the moment all modern browsers, that support strict mode, have // native implementation of Array.prototype.reduce. For instance, IE8 // does not support strict mode, so this check is actually useless. throw new TypeError( 'Array.prototype.reduce called on null or undefined'); } if ('function' !== typeof callback) { throw new TypeError(callback + ' is not a function'); } var index, value, length = this.length >>> 0, isValueSet = false; if (1 < arguments.length) { value = opt_initialValue; isValueSet = true; } for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { if (isValueSet) { value = callback(value, this[index], index, this); } else { value = this[index]; isValueSet = true; } } } if (!isValueSet) { throw new TypeError('Reduce of empty array with no initial value'); } return value; };

    // 使用reduce实例1:计算数组中的0的个数
    function countZeros(array) {
        function counter(total, elem) {
            return total + (elem == 0 ? 1 : 0);
        }
 
        return reduce(counter, 0, array);
    }
 
    alert("countZeros by reduce: " + countZeros([1, 3, 0, 4, 7, 0]));
 
    // 使用reduce实例2:求和
    function sum(array) {
        function add(a, b) {
            return a + b;
        }
 
        return reduce(add, 0, array);
    }
 
    alert("sum by reduce: " + sum([1, 2, 3, 5]));
 
 
    // ------------------------------------------------ //
 
    // 实现map
    function map(func, array) {
        var result = [];
        forEach(array, function(elem) {
            result.push(func(elem));
            // 对于map,func函数一般只有一个参数,所以用func(elem)
        });
 
        return result;
    }
 
    // 利用map实现数组的每个数字翻倍
    var array = [1, 2, 3, 4, 5];
    var mappedArray = map(function(elem) {
        return elem * 2;
    }, array);
    console.log(mappedArray);
 
    // 利用map实现数组向下取整
    var array2 = [1.3, 4.5, 6.7, 8, 9.2];
    var mappedArray2 = map(Math.floor, array2);
    console.log(mappedArray2);
 
 
    // ------------------------------------------------ //
     
    // 实现filter:我自己根据上面两个补充实现的
    function filter(func, array) {
        var result = [];
        forEach(array, function(elem) {
            if(func(elem))
                result.push(elem);
        });
         
        return result;
    }
     
    // 使用filter过滤出偶数
    function isEven(elem) {
        return elem % 2 == 0;
    }
    var array3 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    var filterArray = filter(isEven, array3);
    console.log(filterArray);
     


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
男人,奉劝你永远不要和老婆吵架,原因如下
《嘉莉妹妹》:控制欲望,是成年人最大的自律
excel常用函数公式
婚姻里,解决问题的动力,比方法更重要
喜欢做饭的人,一定要收藏这35点
办公神器,免费!无需注册登录!
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服