Garry
Garry

Reputation: 306

Array indexOf and filter implementation for IE giving other Issues

I am using the following impletations

if (!('forEach' in Array.prototype)) {
            Array.prototype.forEach= function(action, that /*opt*/) {
                for (var i= 0, n= this.length; i<n; i++)
                    if (i in this)
                        action.call(that, this[i], i, this);
            };
        }

        if (!('filter' in Array.prototype)) {
            Array.prototype.filter= function(filter, that /*opt*/) {
                var other= [], v;
                for (var i=0, n= this.length; i<n; i++)
                    if (i in this && filter.call(that, v= this[i], i, this))
                        other.push(v);
                return other;
            };


        }

but when i am using a for loop for ex its acting kind of weird

var conditionJSONObject=new Array();
var conditionCombiner=new Array();
var combiner ;
combiner = jQuery.trim(jQuery(conditionTable.rows[i+1].cells[4].children[0]).select().val());
for(var index in conditionJSONObject)
{               
    if(conditionCombiner[index].combiner.toUpperCase() == 'AND')
    {/*Some code of mine*/}
}

this code gives error though initially it was working fine also same code works fine on FF.when i inspected index value was 'ForEach' ... is it something because of using prototype??

Upvotes: 1

Views: 133

Answers (1)

Vatev
Vatev

Reputation: 7590

Array.prototype.forEach = ... does add a forEach property to all arrays. You can avoid it in for(.. in ..) loops like this:

for(var index in array) {
    if(!array.hasOwnProperty(index)) continue;
    ...//loop body
}

Or you can also use it as indented like this:

array.forEach(function(value,index) {
    ...//loop body
});

Upvotes: 1

Related Questions