分享

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

 文章收藏者 2015-08-25

  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);
     


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章