Array.prototype.find

# Axel Rauschmayer (12 years ago)

It might make sense to add a third argument to that method, so that it works roughly like this:

Array.prototype.find = function (predicate, returnValue = undefined, thisArg = undefined) {
    var arr = Object(this);
    if (typeof predicate !== 'function') {
        throw new TypeError();
    }
    for(var i=0; i < arr.length; i++) {
        if (i in arr) {  // skip holes
            var elem = arr[i];
            if (predicate.call(thisValue, elem, i, arr)) {
                return elem;
            }
        }
    }
    return returnValue;
}
# Jussi Kalliokoski (12 years ago)

What would be the use case for this that isn't covered with Function#bind() or arrow functions?

# Andrea Giammarchi (12 years ago)

I guess just consistency with other Array#forEach/map/every,etc,etc methods plus you don't need to create N bound functions or N arrow functions per each Array#find call ... or not?

# Rick Waldron (12 years ago)

On Thu, Jun 6, 2013 at 4:43 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote:

I guess just consistency with other Array#forEach/map/every,etc,etc methods plus you don't need to create N bound functions or N arrow functions per each Array#find call ... or not?

None of those have a "default return value", so what are you referring to?

# Andrea Giammarchi (12 years ago)

the fact reduce/Right a part all methods accept a thisValue ... I am happy with current method though, still not sure if thisValue will be commonly needed or not.

# Rick Waldron (12 years ago)

On Thu, Jun 6, 2013 at 6:56 PM, Andrea Giammarchi < andrea.giammarchi at gmail.com> wrote:

the fact reduce/Right a part all methods accept a thisValue ... I am happy with current method though, still not sure if thisValue will be commonly needed or not.

Seems irrelevant to the request for a "default return value"

# Axel Rauschmayer (12 years ago)

Note: the proposed new parameter is returnValue, not thisArg (which has already been decided on).

# Jussi Kalliokoski (12 years ago)

Oops, my bad, sorry about that.

# Andrea Giammarchi (12 years ago)

yep, I followed Jussi ... it's his fault :P

sorry I misunderstood too