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

查看:110
本文介绍了为什么.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()不是包含在IE中直到第9版。

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

您可以使用它来实现它:

You could use this to implement it:

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天全站免登陆