1

I've just completed a small form and found out that how I'm using findIndex doesn't work with IE.

Here is an example of the issue.

var people = [
  {name:"Mike", age:"25"},
  {name:"Bill", age:"35"},
  {name:"Terry", age:"44"}
];
console.log(people.findIndex(x => x.name=="Bill" ));

What would be the fastest way to fix this issue for IE?

K3NN3TH
  • 1,458
  • 2
  • 19
  • 31
  • Have you seen [How to fix Array indexOf() in JavaScript for Internet Explorer browsers](http://stackoverflow.com/questions/1744310/how-to-fix-array-indexof-in-javascript-for-internet-explorer-browsers)? – Andrew Morton Sep 12 '16 at 18:43
  • Use a polyfill, such as the one [here](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex). –  Sep 12 '16 at 19:35
  • ^^ + IE11 doesn't support ES6 arrow functions. – Teemu Sep 13 '16 at 05:02

2 Answers2

2

find index support for browsers

    Chrome 45.0 and above: Supports

    Firefox 25.0 and above: Supports

    Internet Explorer: No support

    Microsoft Edge: Supports

    Opera: Supports

    Safari 7.1 and above: Supports

So you need to modify your code something similar to below to work in all browsers.

var index;
for(var i=0;i<people.length;i++){
  if(people[i].name == 'billi'){
   index = i
 }
}

More info

Prashobh
  • 9,216
  • 15
  • 61
  • 91
-3

this is best for IE

people.findIndex(function(x){ x.name=="Bill" }));
mister
  • 95
  • 6