zoukankan      html  css  js  c++  java
  • 数组方法重写:forEach, map, filter, every, some, reduce

    <html>
      <head>
    
      </head>
      <body>
        <script type="text/javascript">
          Array.prototype.myForEach = function(fn) {
            var arr = this,
              len = arr.length,
              arg2 = arguments[1] || window;
            for(var i = 0; i < len; i++) {
              fn.apply(arg2, [arr[i], i, arr]);
            }
          };
    
          Array.prototype.myFilter = function(fn) {
            var arr = this,
              len = arr.length,
              arg2 = arguments[1] || window,
              newArr = [];
            for(var i =0; i < len; i++) {
              var item = deepClone(arr[i]);
              fn.apply(arg2, [item, i, arr])? newArr.push(item): ''
            }
    
            return newArr;
          }
    
          Array.prototype.myMap = function(fn) {
            var arr = this,
              len = arr.length,
              arg2 = arguments[1] || window,
              newArr = [];
            for(var i =0; i < len; i++) {
              var item = deepClone(arr[i]);
              newArr.push(fn.apply(arg2, [item, i, arr]));
            }
            return newArr;
          }
    
          Array.prototype.myReduce = function(fn, initialValue) {
           var arr = this,
            len = arr.length,
            arg2 = arguments[2] || window;
          for(var i = 0; i < len; i++) {
            initialValue = fn.apply(arg2, [initialValue, arr[i], i, arr]);
          }
          return initialValue;
         }
    
         Array.prototype.myEvery = function(fn) {
           var arr = this,
            len = arr.length,
            arg2 = arguments[1] || window,
            flag = true;
          for(var i = 0; i < len; i++) {
            if(!fn.apply(arg2, [arr[i], i, arr])) {
              flag = false;
              break;
            }
          }
          return flag;
         }
    
         Array.prototype.mySome = function(fn) {
           var arr = this,
            len = arr.length,
            arg2 = arguments[1] || window,
            flag = false;
          for(var i = 0; i < len; i++) {
            if(fn.apply(arg2, [arr[i], i, arr])) {
              flag = true;
              break;
            }
          }
          return flag;
         }
    
          function deepClone(source, target) {
            var target = target || {};
            for(var key in source) {
              if(typeof(source[key]) === 'object' && source[key] !== null) {
                if(Object.prototype.toString.call(source[key]) === '[object Object]') {
                  target[key] = {};
                } else if(Object.prototype.toString.call(source[key]) === '[object Array]') {
                  target[key] = [];
                }
                deepClone(source[key], target[key]);
              } else {
                target[key] = source[key]
              }
            }
            return target;
          }
    
          var arr = [{id: 1}, {id: 2}];
          arr.myForEach(function(item, index, myArr){
            item.id = item.id +1;
          });
    
          var arrFilter = arr.myFilter(function(item, index, myArr) {
            return item.id > 2;
          });
    
          var arrMap = arr.myFilter(function(item, index, myArr) {
            return item.id = item.id + 2;
          });
    
    
        var arr1 = [1, 3, 5, 6];
         console.log(arr1.myReduce(function(prev, elem){
           return prev = prev + elem;
         }, 0));
         console.log(arr1.myEvery(function(item){
           return item > 2;
         }));
         console.log(arr1.mySome(function(item){
           return item > 2;
         }));
    
          console.log(arr);
    
          var obj1 = {
            a: 1,
            b: {
              c: {
                d: 4
              }
            },
            e: null,
            f: undefined,
            g: false,
            h: [{id: 1}, 2, {child: {name: 'test'}}]
          };
          var obj2 = {};
          deepClone(obj1, obj2);
          console.log(obj2);
        </script>
      </body>
    </html>
  • 相关阅读:
    CentOS下安装中文man 手册
    CentOS 6.5系统安装配置图解教程
    a链接点击下载图片到本地(php)
    PHP 常用的header头部定义汇总
    thinkphp3.2接入支付宝支付接口(PC端)
    thinkphp3.2.3多图上传并且生成多张缩略图
    利用<meta http-equiv="refresh" content="0;URL=?id='.$id.'" />一条一条的更新数据
    【C/C++】C语言内存模型 (C memory layout)
    【软件工程】关于编程思想、学习方法
    【Python】opencv-python入门
  • 原文地址:https://www.cnblogs.com/ycherry/p/12106123.html
Copyright © 2011-2022 走看看