0

I was using indexOf method on top of an array in IE 8 and it gave error (as it is not suported). I opted to use underscore.js library. I used the _.indexOf(array, value, [isSorted]) from underscorejs library. But indexOf() of underscore.js library is case sensitive but I want to ignore the case while comparing the value. How can I ignore case while using this function.

I can always use for loop to loop over the array and then use toLowerCase() but I wonder if there is out of box method which can do this for me.

SharpCoder
  • 18,279
  • 43
  • 153
  • 249

2 Answers2

1

I would suggest you to make the string to lower case and then check the index of lower cased string.

Like:

"Hi I am string".toLowerCase().indexOf("Hi".toLowerCase());
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1

I can always use for loop to loop over the array and then use toLowerCase()

That's basically the way to go.

but I wonder if there is out of box method which can do this for me.

No, yet you don't have to use a for-loop if you use this indexOf variant with an iterator callback.

Since I don't know what you do need the index for, maybe _.find or _.some do work as well for you.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375