Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts

    IE8 : Javascript Filter Method Object does not support this property or method

    Hi Guys,

    I am trying to use filter method to get distinct selected element in an array in my code. but i have issue calling that in IE 8 browser. "Object does not support this property or method " .

    Is there an equivalent method or a workaround ?

    Thanks

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Some people call it shimming-

    Code:
    (function(){
    	var AP=Array.prototype, AP2={
    		every: function(fun, scope){
    			var T= this, len= T.length;
    			if(typeof fun== 'function'){
    				for(var i= 0; i < len; i++){
    					if(i in T){
    						if(fun.call(scope, T[i], i, T)== false) return false;
    					}
    				}
    				return true;
    			}
    			return false;
    		},
    		filter: function(fun, scope){
    			var T= this, A= [], i= 0, itm, L= T.length;
    			if(typeof fun== 'function'){
    				while(i< L){
    					if(i in T){
    						itm= T[i];
    						if(fun.call(scope, itm, i, T)) A[A.length]= itm;
    					}
    					++i;
    				}
    			}
    			return A;
    		},
    		forEach: function(fun, scope){
    			var T= this, L= T.length, i= 0;
    			if(typeof fun== 'function'){
    				while(i< L){
    					if(i in T){
    						fun.call(scope, T[i], i, T);
    					}
    					++i;
    				}
    			}
    			return T;
    		},
    		indexOf: function(what, i){
    			if(!i || typeof i!= 'number') i= 0;
    			var L= this.length;
    			while(i<L){
    				if(this[i]=== what) return i;
    				++i;
    			}
    			return -1;
    		},
    		lastIndexOf: function(what, i){
    			var L= this.length;
    			i= i || L-1;
    			if(isNaN(i) || i>= L) i= L-1;
    			if(i< 0) i += L;
    			while(i> -1){
    				if(this[i]=== what) return i;
    				--i;
    			}
    			return -1;
    		},
    		map: function(fun, scope){
    			var T= this, L= T.length, A= Array(L), i= 0;
    			if(typeof fun== 'function'){
    				while(i< L){
    					if(i in T){
    						A[i]= fun.call(scope, T[i], i, T);
    					}
    					++i;
    				}
    				return A;
    			}
    		},
    		reduce: function(fun, temp, scope){
    			var T= this, i= 0, len= T.length, temp;
    			if(typeof fun=== 'function'){
    				if(temp== undefined) temp= T[i++];
    				while(i < len){
    					if(i in T) temp= fun.call(scope, temp, T[i], i, T);
    					i++;
    				}
    			}
    			return temp;
    		},
    		reduceRight: function(fun, temp, scope){
    			var T= this.concat().reverse();
    			return T.reduce(fun, temp, scope);
    		},
    		some: function(fun, scope){
    			var T= this, L= T.length;
    			if(typeof fun== 'function'){
    				for(var i= 0; i < L; i++){
    					if(i in T && fun.call(scope, T[i], i, T)){
    						return true;
    					}
    				}
    			}
    			return false;
    		}
    	}
    	for(var p in AP2){
    		if(!AP[p]) AP[p]= AP2[p];
    	}
    })();

  • Users who have thanked mrhoo for this post:

    korssane (02-07-2013)

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hi mrhoo,
    thanks for the quick reply. that code scares me..
    all that code is for the filter method ?

    thanks

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Code:
    if(!Array.prototype.filter){
    	Array.prototype.filter=  function(fun, scope){
    		var T= this, A= [], i= 0, itm, L= T.length;
    		if(typeof fun== 'function'){
    			while(i<L){
    				if(i in T){
    					itm= T[i];
    					if(fun.call(scope, itm, i, T)) A[A.length]= itm;
    				}
    				++i;
    			}
    		}
    		return A;
    	}
    }
    My previous post included filter with the other Array methods IE does not support before version 9.
    Last edited by mrhoo; 02-06-2013 at 04:31 PM.

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    hi mrhoo,

    but should this work now on IE8 as iam using that now..?

    thanks

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by korssane View Post
    but should this work now on IE8 as iam using that now..?
    Just add the code mrhoo provided and IE8 will use that to do the filter while all other browsers will continue to use the built in code. The first line tests if the method already exists and only adds the method if it doesn't.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • The Following 2 Users Say Thank You to felgall For This Useful Post:

    korssane (02-07-2013), mrhoo (02-07-2013)

  • #7
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Hi felgall,

    Are you talking about the 1st piece of code or the 2nd one ?

    thanks

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,618
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by korssane View Post
    Hi felgall,

    Are you talking about the 1st piece of code or the 2nd one ?

    thanks
    The first piece of code includes all of the new array methods that IE8 doesn't support until you add that code. The second piece of code is that part of the first piece of code that specifically adds filter()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    thanks guys. It really helps. I will close this thread.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •