user782104
user782104

Reputation: 13555

Data.filter not working in IE 8?

 countImage = data.filter(function(value) { return value !== undefined }).length;

This statement return error of Object doesn't support this property or method, how to fix the problem ? thanks

Update, data is get from ajax and it is an array encoded using json

$imgArray[] = $dir.DIRECTORY_SEPARATOR.$file.DIRECTORY_SEPARATOR.'Pv'.$filePadded.'.png';
die(json_encode($imgArray));

data: {'data':issueString}, 
                success: function (data) {
                    countImage = data.filter(function(value) { return value !== undefined }).length;


..........

Upvotes: 0

Views: 1657

Answers (2)

Nikanos Polykarpou
Nikanos Polykarpou

Reputation: 391

What you get is a javascript array and not a jquery object so you are calling on the Array.prototype.filter method.

If you see the compatibility for IE it is mentioned that it is supported in IE9+.

In that page there is also a method to use for browsers that do not support Array.prototype.filter

if (!Array.prototype.filter)
{
  Array.prototype.filter = function(fun /*, thisp */)
  {
    "use strict";

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

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388376

array.filter() is not available for IE < 10, it is available in Chrome, FF and IE 10. so you need to use some other alternates to filter the array.

You can use jQuery.grep instead of fitler method

Code

var a = [1, 2, 3, 4, 5, undefined, 6, 7, undefined, 8];
alert(a.length);
var arr = jQuery.grep(a, function(n, i){
  return n != undefined;
});

alert(arr.length);

fiddle

Upvotes: 1

Related Questions