Skip to content

Instantly share code, notes, and snippets.

@rwaldron rwaldron/find.md
Last active Dec 14, 2015

Embed
What would you like to do?
ES6 Array.prototype.find spec

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

@mathiasbynens

This comment has been minimized.

Copy link

commented Mar 15, 2013

@paulmillr

This comment has been minimized.

Copy link

commented Mar 18, 2013

ES6-shim has this now paulmillr/es6-shim@197a9a0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.