Javascript Array findIndex(fn, v)

Description

Javascript Array findIndex(fn, v)


Array.prototype.findIndex = function(fn, v) {
    var isFN = typeof(fn) === 'function';
    var isV = v !== undefined;
    for (var i = 0, len = this.length; i < len; i++) {
        if (isFN) {
            if (fn(this[i], i)) {
                return i;
            }//from ww  w .ja  v  a  2  s. c  o m
            continue;
        }
        if (isV) {
            if (this[i] && this[i][fn] === v) {
                return i;
            }
            continue;
        }
        if (this[i] === fn) {
            return i;
        }
    }
    return -1;
};



PreviousNext

Related