您的位置:首页 > 其它

解决ie11以下不兼容array数组的findIndex方法

2020-02-04 07:49 330 查看

解决ie11以下不兼容array数组的findIndex方法

最近在做项目的兼容问题,总是碰到令人头疼的各种不支持问题。真的是。。。
下面说一下关于ie11以下不兼容array数组的findIndex方法的解决方案

// 兼容ie数组没有findIndex方法
if (!Array.prototype.findIndex) {
Object.defineProperty(Array.prototype, 'findIndex', {
value: function(predicate:any) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var o = Object(this);

// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;

// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}

// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
var thisArg = arguments[1];

// 5. Let k be 0.
var k = 0;

// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return k.
var kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return k;
}
// e. Increase k by 1.
k++;
}

// 7. Return -1.
return -1;
}
});
}
  • 点赞 2
  • 收藏
  • 分享
  • 文章举报
一剑惊鸿影 发布了1 篇原创文章 · 获赞 6 · 访问量 1276 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐