1

Is there an alternative to using array.some()? I've had it in my code for a while but I just learned that its not supported by IE8.

My original code:

var list = ['Johny', 'Adam', 'Johny'];
var option = 'Johny';

var foundInList = list.some(function (el) {
    return el === option;
});

I could replace it with something like :

var test = false;
for (i = 0; i < list.length; i++){
    if (list[i] === option){
        test = true;
        break;
    }
}

But there may be a better way to do the same.

Please help. Thanks in Advance

PS: Here's a fiddle

Johny
  • 387
  • 1
  • 7
  • 20
  • 1
    you can refer http://stackoverflow.com/questions/2790001/fixing-javascript-array-functions-in-internet-explorer-indexof-foreach-etc and see the polyfill section here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some – ankur May 22 '15 at 17:42

2 Answers2

3

I think your best solution here is just a bespoke for loop over the entries. Most array mutation methods are not supported in IE8. I would recommend not polyfilling to replace the function because your polyfill may not efficiently replace the function in modern browsers.

BTC
  • 3,802
  • 5
  • 26
  • 39
1

You can add your own some method

if (!Array.prototype.some)
{
    Array.prototype.some = function (func)
    {
        for (var i in this)
        {
           if (func(i)) return true;
        }
        return false;
    };
}
JoshBerke
  • 66,142
  • 25
  • 126
  • 164