Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2013, 03:59 PM   PM User | #1
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
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
korssane is offline   Reply With Quote
Old 02-06-2013, 04:11 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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];
	}
})();
mrhoo is offline   Reply With Quote
Users who have thanked mrhoo for this post:
korssane (02-07-2013)
Old 02-06-2013, 04:13 PM   PM User | #3
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
Hi mrhoo,
thanks for the quick reply. that code scares me..
all that code is for the filter method ?

thanks
korssane is offline   Reply With Quote
Old 02-06-2013, 04:29 PM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
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..
mrhoo is offline   Reply With Quote
Old 02-06-2013, 05:38 PM   PM User | #5
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
hi mrhoo,

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

thanks
korssane is offline   Reply With Quote
Old 02-06-2013, 06:35 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,459
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is online now   Reply With Quote
The Following 2 Users Say Thank You to felgall For This Useful Post:
korssane (02-07-2013), mrhoo (02-07-2013)
Old 02-06-2013, 07:09 PM   PM User | #7
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
Hi felgall,

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

thanks
korssane is offline   Reply With Quote
Old 02-06-2013, 09:37 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,459
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is online now   Reply With Quote
Old 02-07-2013, 12:48 AM   PM User | #9
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
thanks guys. It really helps. I will close this thread.
korssane is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:29 PM.


Advertisement
Log in to turn off these ads.