为什么 .filter() 在 Internet Explorer 8 中不起作用? [英] Why won't .filter() work in Internet Explorer 8?

查看:33
本文介绍了为什么 .filter() 在 Internet Explorer 8 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一行:

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

这是错误:

对象不支持此属性或方法

Object doesn't support this property or method

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

This Works 100% fine in Chrome. What's going on?

推荐答案

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

Array.filter() isn't included in IE until version 9.

你可以用它来实现它:

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 对象:

Or since you are using jQuery, you can wrap your array into a jQuery object first:

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

这篇关于为什么 .filter() 在 Internet Explorer 8 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆