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

2024-02-25

这是行:

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

这是错误:

对象不支持此属性或方法

这在 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;
  };
}

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

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

songs = $(songs).filter(function(){
  return this.album==album;
}); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 .filter() 在 Internet Explorer 8 中不起作用? 的相关文章

随机推荐