Skip to content

Commit 5f31dd6

Browse files
Kingwlljharb
authored andcommitted
Normative: Add array-find-from-last (#2476)
1 parent 3ea9275 commit 5f31dd6

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

spec.html

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37384,6 +37384,62 @@ <h1>Array.prototype.findIndex ( _predicate_ [ , _thisArg_ ] )</h1>
3738437384
</emu-note>
3738537385
</emu-clause>
3738637386

37387+
<emu-clause id="sec-array.prototype.findlast">
37388+
<h1>Array.prototype.findLast ( _predicate_ [ , _thisArg_ ] )</h1>
37389+
<emu-note>
37390+
<p>_predicate_ should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. `findLast` calls _predicate_ once for each element of the array, in descending index order, until it finds one where _predicate_ returns *true*. If such an element is found, `findLast` immediately returns that element value. Otherwise, `findLast` returns *undefined*.</p>
37391+
<p>If a _thisArg_ parameter is provided, it will be used as the *this* value for each invocation of _predicate_. If it is not provided, *undefined* is used instead.</p>
37392+
<p>_predicate_ is called with three arguments: the element, the index of the element in the array, and the object being traversed.</p>
37393+
<p>`findLast` does not directly mutate the object on which it is called but the object may be mutated by the calls to _predicate_.</p>
37394+
<p>The range of elements processed by `findLast` is set before the first call to _predicate_. Elements that are appended to the array after the call to `findLast` begins will not be visited by _predicate_. If existing elements of the array are changed, their value as passed to _predicate_ will be the value at the time that `findLast` visits them.</p>
37395+
</emu-note>
37396+
<p>When the `findLast` method is called, the following steps are taken:</p>
37397+
<emu-alg>
37398+
1. Let _O_ be ? ToObject(*this* value).
37399+
1. Let _len_ be ? LengthOfArrayLike(_O_).
37400+
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
37401+
1. Let _k_ be _len_ - 1.
37402+
1. Repeat, while _k_ &ge; 0,
37403+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
37404+
1. Let _kValue_ be ? Get(_O_, _Pk_).
37405+
1. Let _testResult_ be ToBoolean(? Call(_predicate_, _thisArg_, &laquo; _kValue_, 𝔽(_k_), _O_ &raquo;)).
37406+
1. If _testResult_ is *true*, return _kValue_.
37407+
1. Set _k_ to _k_ - 1.
37408+
1. Return *undefined*.
37409+
</emu-alg>
37410+
<emu-note>
37411+
<p>The `findLast` 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.</p>
37412+
</emu-note>
37413+
</emu-clause>
37414+
37415+
<emu-clause id="sec-array.prototype.findlastindex">
37416+
<h1>Array.prototype.findLastIndex ( _predicate_ [ , _thisArg_ ] )</h1>
37417+
<emu-note>
37418+
<p>_predicate_ should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. `findLastIndex` calls _predicate_ once for each element of the array, in descending index order, until it finds one where _predicate_ returns *true*. If such an element is found, `findLastIndex` immediately returns the index of that element value. Otherwise, `findLastIndex` returns -1.</p>
37419+
<p>If a _thisArg_ parameter is provided, it will be used as the *this* value for each invocation of _predicate_. If it is not provided, *undefined* is used instead.</p>
37420+
<p>_predicate_ is called with three arguments: the element, the index of the element in the array, and the object being traversed.</p>
37421+
<p>`findLastIndex` does not directly mutate the object on which it is called but the object may be mutated by the calls to _predicate_.</p>
37422+
<p>The range of elements processed by `findLastIndex` is set before the first call to _predicate_. Elements that are appended to the array after the call to `findLastIndex` begins will not be visited by _predicate_. If existing elements of the array are changed, their value as passed to _predicate_ will be the value at the time that `findLastIndex` visits them.</p>
37423+
</emu-note>
37424+
<p>When the `findLastIndex` method is called, the following steps are taken:</p>
37425+
<emu-alg>
37426+
1. Let _O_ be ? ToObject(*this* value).
37427+
1. Let _len_ be ? LengthOfArrayLike(_O_).
37428+
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
37429+
1. Let _k_ be _len_ - 1.
37430+
1. Repeat, while _k_ &ge; 0,
37431+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
37432+
1. Let _kValue_ be ? Get(_O_, _Pk_).
37433+
1. Let _testResult_ be ToBoolean(? Call(_predicate_, _thisArg_, &laquo; _kValue_, 𝔽(_k_), _O_ &raquo;)).
37434+
1. If _testResult_ is *true*, return 𝔽(_k_).
37435+
1. Set _k_ to _k_ - 1.
37436+
1. Return *-1*<sub>𝔽</sub>.
37437+
</emu-alg>
37438+
<emu-note>
37439+
<p>The `findLastIndex` 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.</p>
37440+
</emu-note>
37441+
</emu-clause>
37442+
3738737443
<emu-clause id="sec-array.prototype.flat">
3738837444
<h1>Array.prototype.flat ( [ _depth_ ] )</h1>
3738937445
<p>When the `flat` method is called, the following steps are taken:</p>
@@ -38203,6 +38259,8 @@ <h1>Array.prototype [ @@unscopables ]</h1>
3820338259
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"fill"*, *true*).
3820438260
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"find"*, *true*).
3820538261
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"findIndex"*, *true*).
38262+
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"findLast"*, *true*).
38263+
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"findLastIndex"*, *true*).
3820638264
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"flat"*, *true*).
3820738265
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"flatMap"*, *true*).
3820838266
1. Perform ! CreateDataPropertyOrThrow(_unscopableList_, *"includes"*, *true*).
@@ -38876,6 +38934,48 @@ <h1>%TypedArray%.prototype.findIndex ( _predicate_ [ , _thisArg_ ] )</h1>
3887638934
<p>This function is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
3887738935
</emu-clause>
3887838936

38937+
<emu-clause id="sec-%typedarray%.prototype.findlast">
38938+
<h1>%TypedArray%.prototype.findLast ( _predicate_ [ , _thisArg_ ] )</h1>
38939+
<p>The interpretation and use of the arguments of %TypedArray%`.prototype.findLast` are the same as for `Array.prototype.findLast` as defined in <emu-xref href="#sec-array.prototype.findlast"></emu-xref>.</p>
38940+
<p>When the `findLast` method is called, the following steps are taken:</p>
38941+
<emu-alg>
38942+
1. Let _O_ be the *this* value.
38943+
1. Perform ? ValidateTypedArray(_O_).
38944+
1. Let _len_ be _O_.[[ArrayLength]].
38945+
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
38946+
1. Let _k_ be _len_ - 1.
38947+
1. Repeat, while _k_ &ge; 0,
38948+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
38949+
1. Let _kValue_ be ! Get(_O_, _Pk_).
38950+
1. Let _testResult_ be ToBoolean(? Call(_predicate_, _thisArg_, &laquo; _kValue_, 𝔽(_k_), _O_ &raquo;)).
38951+
1. If _testResult_ is *true*, return _kValue_.
38952+
1. Set _k_ to _k_ - 1.
38953+
1. Return *undefined*.
38954+
</emu-alg>
38955+
<p>This function is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
38956+
</emu-clause>
38957+
38958+
<emu-clause id="sec-%typedarray%.prototype.findlastindex">
38959+
<h1>%TypedArray%.prototype.findLastIndex ( _predicate_ [ , _thisArg_ ] )</h1>
38960+
<p>The interpretation and use of the arguments of %TypedArray%`.prototype.findLastIndex` are the same as for `Array.prototype.findLastIndex` as defined in <emu-xref href="#sec-array.prototype.findlastindex"></emu-xref>.</p>
38961+
<p>When the `findLastIndex` method is called, the following steps are taken:</p>
38962+
<emu-alg>
38963+
1. Let _O_ be the *this* value.
38964+
1. Perform ? ValidateTypedArray(_O_).
38965+
1. Let _len_ be _O_.[[ArrayLength]].
38966+
1. If IsCallable(_predicate_) is *false*, throw a *TypeError* exception.
38967+
1. Let _k_ be _len_ - 1.
38968+
1. Repeat, while _k_ &ge; 0,
38969+
1. Let _Pk_ be ! ToString(𝔽(_k_)).
38970+
1. Let _kValue_ be ! Get(_O_, _Pk_).
38971+
1. Let _testResult_ be ToBoolean(? Call(_predicate_, _thisArg_, &laquo; _kValue_, 𝔽(_k_), _O_ &raquo;)).
38972+
1. If _testResult_ is *true*, return 𝔽(_k_).
38973+
1. Set _k_ to _k_ - 1.
38974+
1. Return *-1*<sub>𝔽</sub>.
38975+
</emu-alg>
38976+
<p>This function is not generic. The *this* value must be an object with a [[TypedArrayName]] internal slot.</p>
38977+
</emu-clause>
38978+
3887938979
<emu-clause id="sec-%typedarray%.prototype.foreach">
3888038980
<h1>%TypedArray%.prototype.forEach ( _callbackfn_ [ , _thisArg_ ] )</h1>
3888138981
<p>The interpretation and use of the arguments of %TypedArray%`.prototype.forEach` are the same as for `Array.prototype.forEach` as defined in <emu-xref href="#sec-array.prototype.foreach"></emu-xref>.</p>

0 commit comments

Comments
 (0)