Javascript Array indexOf(obj, fromIndex)

Description

Javascript Array indexOf(obj, fromIndex)


/** Setup indexOf and contains methods **/
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (obj, fromIndex) {
        if (fromIndex == null) {
            fromIndex = 0;/*w  ww  . ja  v  a 2 s.com*/
        } else if (fromIndex < 0) {
            fromIndex = Math.max(0, this.length + fromIndex);
        }
        for (var i = fromIndex, j = this.length; i < j; i++) {
            if (this[i] === obj) return i;
        }
        return -1;
    };
}

Javascript Array indexOf(obj, fromIndex)

Array.prototype.indexOf = function (obj, fromIndex) {
    for (var i = (fromIndex || 0); i < this.length; i++) {
        if (this[i] === obj) {
            return i;
        }//  ww w  .j  av a 2  s  .co m
    }
    return -1;
};



PreviousNext

Related