js .filter/.find不兼容ie

.filter:

https://blog.csdn.net/weixin_30784945/article/details/95575670

Array.prototype.myfilter = function(fun /*, thisp*/){  
    var len = this.length;  
    if (typeof fun != "function"){  
        throw new TypeError();  
    }  
    var res = new Array();  
    var thisp = arguments[1];  
    for (var i = 0; i < len; i++){  
        if (i in this){  
            var val = this[i]; // in case fun mutates this  
            if (fun.call(thisp, val, i, this)) {  
                res.push(val);  
            }  
        }  
    }  
    return res;  
}; 

var tina = ArrData.myfilter(function(item, inde, array){
    return item.mapid == 5; //过滤条件,根据自己需求修改
  })

  .find

https://www.cnblogs.com/huangtailang/p/7273980.html

//没有该方法时,自定义
if(!Array.prototype.find){
    Array.prototype.find = function(callback) {
        return callback && (this.filter(callback)|| [])[0];
    };
}

//demo
var s = [{"name":"001"},{"name":"002"},{"name":"003"}];
s.find(function(a){
  return a.name=='001'
});

  

posted @ 2020-03-06 13:37  yomi丶  阅读(1626)  评论(0编辑  收藏  举报