Javascript Array find(fn, v)

Description

Javascript Array find(fn, v)


Array.prototype.find = 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 this[i];
            }/*ww w.j  a v a 2  s. c  om*/
            continue;
        }
        if (isV) {
            if (this[i] && this[i][fn] === v) {
                return this[i];
            }
            continue;
        }
        if (this[i] === fn) {
            return this[i];
        }
    }
    return null;
};



PreviousNext

Related