Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't .filter() work in Internet Explorer 8?

This is the line:

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

This is the error:

Object doesn't support this property or method

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

like image 555
Jack B Avatar asked Aug 22 '11 20:08

Jack B


People also ask

Does filter work in IE?

Internet Explorer has a whole bunch of filters for various effects. Unfortunately, the Shadow filter created specifically for making shadows gives an incredibly poor result. But there is a trick we can use here: filter effects can be overlayed on top of each other. progid: DXImageTransform.

Why JavaScript is not working in Internet Explorer?

Internet Explorer When the "Internet Options" window opens, select the Security tab. On the "Security" tab, make sure the Internet zone is selected, and then click on the "Custom level..." button. In the Security Settings – Internet Zone dialog box, click Enable for Active Scripting in the Scripting section.

How does filter function work in JavaScript?

Definition and Usage. The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

How do you filter an array of objects?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.


1 Answers

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

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

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

songs = $(songs).filter(function(){   return this.album==album; });  
like image 77
Paul Avatar answered Oct 11 '22 03:10

Paul