gpt4 book ai didi

JavaScript 过滤器在 IE9 及更低版本中不起作用

转载 作者:行者123 更新时间:2023-12-03 08:09:49 26 4
gpt4 key购买 nike

我在 IE 8 中收到以下错误

对象不支持此属性或方法

Helper.prototype.getUniqueArray = function(a) {
/*console.log(a);*/
return a.filter(function(elem, pos, self) {
if (elem === '') {
return false;
}
return self.indexOf(elem) === pos;
});
};

请帮助我在 IE9 及更低版本中实现此功能。

最佳答案

按照 MDN docs 中的指定对 .filter() 使用 pollyfill :

Polyfill

filter() was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of filter() in ECMA-262 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming that fn.call evaluates to the original value of Function.prototype.call(), and that Array.prototype.push() has its original value.

if (!Array.prototype.filter) {
Array.prototype.filter = function(fun/*, thisArg*/) {
'use strict';

if (this === void 0 || this === null) {
throw new TypeError();
}

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}

var res = [];
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i];

// NOTE: Technically this should Object.defineProperty at
// the next index, as push can be affected by
// properties on Object.prototype and Array.prototype.
// But that method's new, and collisions should be
// rare, so use the more-compatible alternative.
if (fun.call(thisArg, val, i, t)) {
res.push(val);
}
}
}

return res;
};
}

关于JavaScript 过滤器在 IE9 及更低版本中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com