« Bugzilla Issues Index

#1342 — Array.prototype.find( predicate [ , thisArg ] )

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.

1. Let O be the result of ToObject passing the this value as the argument.
2. ReturnIfAbrupt( O ).
3. Let lenValue be the result of Get( O, "length" ).
4. Let len be ToUint32( lenValue ).
5. ReturnIfAbrupt( len ).
6. If len is 0, return undefined.
7. If IsCallable( predicate ) is false, throw a TypeError exception.
8. If thisArg was supplied, let T be thisArg; else let T be undefined
9. Let k be 0.
10. 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.
11. 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

Are the initial algorithm steps on purpose different from the other Array helper functions? Specifically I refer to step 6-7.

added in rev15 editor's draft

resolved in rev 15, May 14, 2013 draft