15.4.4.x Array.prototype.find ( predicate [ , thisArg ] )
predicate should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. find calls predicate once for each element present in the array, in ascending order, until predicate returns true and immediately returns the current array element. Otherwise, find returns null. predicate is called only for elements of the array which actually exist; it is not called for missing elements of the array.
- Let O be the result of ToObject passing the this value as the argument.
- ReturnIfAbrupt( O ).
- Let lenValue be the result of Get( O, "length" ).
- Let len be ToUint32( lenValue ).
- ReturnIfAbrupt( len ).
- If len is 0, return undefined.
- If IsCallable( predicate ) is false, throw a TypeError exception.
- If thisArg was supplied, let T be thisArg; else let T be undefined
- Let k be 0.
- Repeat, while k < len
- a. Let Pk be ToString( k )
- b. Let kPresent be the result of HasProperty( O, Pk ).
- c. ReturnIfAbrupt( kPresent )
- d. If kPresent is true, then
- i. Let kValue be the result of calling Get( O, Pk ).
- ii. ReturnIfAbrupt( kValue ).
- iii. Let result be the result of calling the [[Call]] internal method of predicate with T as thisArgument and a List containing kValue, k, and O as argumentsList.
- iv. ReturnIfAbrupt( result ).
- v. If ToBoolean( result ) is true, return kValue.
- e. Increase k by 1.
- Return undefined.
The length property of the find method is 1.
NOTE The find function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether the find function can be applied successfully to a host object is implementation-dependent