Stage 4 Draft / June 6, 2022

Proposal-array-find-from-last

1 Array.prototype.findLast ( predicate [ , thisArg ] )

Note 1

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 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.

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.

predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.

findLast does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.

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.

When the findLast method is called, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(predicate) is false, throw a TypeError exception.
  4. Let k be len - 1.
  5. Repeat, while k ≥ 0,
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ? Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
    4. If testResult is true, return kValue.
    5. Set k to k - 1.
  6. Return undefined.
Note 2

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.

2 Array.prototype.findLastIndex ( predicate [ , thisArg ] )

Note 1

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 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.

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.

predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.

findLastIndex does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.

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.

When the findLastIndex method is called, the following steps are taken:

  1. Let O be ? ToObject(this value).
  2. Let len be ? LengthOfArrayLike(O).
  3. If IsCallable(predicate) is false, throw a TypeError exception.
  4. Let k be len - 1.
  5. Repeat, while k ≥ 0,
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ? Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
    4. If testResult is true, return 𝔽(k).
    5. Set k to k - 1.
  6. Return -1𝔽.
Note 2

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.

3 Array.prototype [ @@unscopables ]

The initial value of the @@unscopables data property is an object created by the following steps:

  1. Let unscopableList be OrdinaryObjectCreate(null).
  2. Perform ! CreateDataPropertyOrThrow(unscopableList, "copyWithin", true).
  3. Perform ! CreateDataPropertyOrThrow(unscopableList, "entries", true).
  4. Perform ! CreateDataPropertyOrThrow(unscopableList, "fill", true).
  5. Perform ! CreateDataPropertyOrThrow(unscopableList, "find", true).
  6. Perform ! CreateDataPropertyOrThrow(unscopableList, "findIndex", true).
  7. Perform ! CreateDataPropertyOrThrow(unscopableList, "findLast", true).
  8. Perform ! CreateDataPropertyOrThrow(unscopableList, "findLastIndex", true).
  9. Perform ! CreateDataPropertyOrThrow(unscopableList, "flat", true).
  10. Perform ! CreateDataPropertyOrThrow(unscopableList, "flatMap", true).
  11. Perform ! CreateDataPropertyOrThrow(unscopableList, "includes", true).
  12. Perform ! CreateDataPropertyOrThrow(unscopableList, "keys", true).
  13. Perform ! CreateDataPropertyOrThrow(unscopableList, "values", true).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

4 %TypedArray%.prototype.findLast ( predicate [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.findLast are the same as for Array.prototype.findLast as defined in 1.

When the findLast method is called, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(predicate) is false, throw a TypeError exception.
  5. Let k be len - 1.
  6. Repeat, while k ≥ 0,
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
    4. If testResult is true, return kValue.
    5. Set k to k - 1.
  7. Return undefined.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

5 %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] )

The interpretation and use of the arguments of %TypedArray%.prototype.findLastIndex are the same as for Array.prototype.findLastIndex as defined in 2.

When the findLastIndex method is called, the following steps are taken:

  1. Let O be the this value.
  2. Perform ? ValidateTypedArray(O).
  3. Let len be O.[[ArrayLength]].
  4. If IsCallable(predicate) is false, throw a TypeError exception.
  5. Let k be len - 1.
  6. Repeat, while k ≥ 0,
    1. Let Pk be ! ToString(𝔽(k)).
    2. Let kValue be ! Get(O, Pk).
    3. Let testResult be ! ToBoolean(? Call(predicate, thisArg, « kValue, 𝔽(k), O »)).
    4. If testResult is true, return 𝔽(k).
    5. Set k to k - 1.
  7. Return -1𝔽.

This function is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.

A Copyright & Software License

Copyright Notice

© 2022 Wenlu Wang, Daniel Rosenwasser

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.