gpt4 book ai didi

javascript - 为什么 .filter() 在 Internet Explorer 8 中不起作用?

转载 作者:行者123 更新时间:2023-12-03 21:35:28 25 4
gpt4 key购买 nike

这是一行:

songs = songs.filter(function (el) {
return el.album==album;
});

这是错误:

Object doesn't support this property or method

这在 Chrome 中 100% 正常工作。这是怎么回事?

最佳答案

Array.filter() 直到版本 9 才包含在 IE 中。

您可以使用它来实现它:

if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp */)
{
"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 thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}

return res;
};
}

来自:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter

或者,由于您使用的是 jQuery,因此您可以先将数组包装到 jQuery 对象中:

songs = $(songs).filter(function(){
return this.album==album;
});

关于javascript - 为什么 .filter() 在 Internet Explorer 8 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7153470/

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