Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE: indexOf results in "object doesn't support this property or method"

I have the following if statement:

if (buyArray.indexOf(dealWith,0) != -1){ 

Which is breaking in ie (ie 8 on XP) with "object doesn't support this property or method".

Anyone have a work around for this?

like image 235
Fraser Avatar asked Jun 06 '12 22:06

Fraser


People also ask

Does indexOf work with objects?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

Does indexOf only work on strings?

Yes, indexOf() works with array (of number, string, or any object), as well as with string.

What is the function of the indexOf () in arrays?

indexOf() The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

What does it mean when indexOf returns 0?

JavaScript String indexOf() It defaults to zero (0), meaning that if you omit the fromIndex , the search will start from the beginning of the string. The indexOf() always perform a case-sensitive search. To find the index of the last occurrence of a substring in a string, you use the lastIndexOf() method.


1 Answers

yeah, IE<9 doesn't support indexOf. You can implement a shim like the one showed here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

Or if you already using jQuery you can use inArray. Also underscore has an implementation for it.

like image 75
ZER0 Avatar answered Sep 28 '22 03:09

ZER0