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

这是一行:

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

这是错误:

Object不支持此属性或方法

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

在版本9之前,IE中不包含Array.filter()

您可以使用它来实现它:

 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; }); 

使用es5-shim,所以你可以在IE8中使用filter / indexOf!

Facebook的react.js也使用它。

   

https://github.com/es-shims/es5-shim

使用attr()函数是否有效?

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