predicate should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. findIndex calls predicate once for each element present in the array, in ascending order, until predicate returns true and immediately returns the index of the current array element. Otherwise, findIndex return -1. 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 -1.
- 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 k.
- e. Increase k by 1.
- Return -1.
The length property of the findIndex method is 1.
NOTE The findIndex 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 findIndex function can be applied successfully to a host object is implementation-dependent
Quick and dirty polyfill by @WebReflection: https://gist.github.com/WebReflection/5167299