Skip to content
Richard L. Hudson edited this page Apr 3, 2014 · 2 revisions

Array.prototype.filterPar ( callbackfun, thisArg = undefined )

callbackfun should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. filterPar calls callbackfun once for each element in the array, in an arbitrary implementation dependent order, and constructs a new array of all the values for which callbackfun returns true. callbackfun is called only for elements of the array which actually exist; it is not called for missing elements of the array.

If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfun. If it is not provided, undefined is used instead.

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

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

The range of elements processed by filterPar is set before the first call to callbackfun. Elements which are appended to the array after the call to filter begins will not be visited by callbackfun. If existing elements of the array are changed their value as passed to callbackfun will be the value at the time filterPar visits them; elements that are deleted after the call to filterPar begins and before being visited are not visited.

When the filter method is called with one or two arguments, the following steps are taken:

  1. Let O be the result of calling 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 ToLength(lenValue).
  5. ReturnIfAbrupt(len).
  6. If IsCallable(callbackfun) is false, throw a TypeError exception.
  7. If thisArg was supplied, let T be thisArg; else let T be undefined.
  8. Let A be undefined.
  9. If O is an exotic Array object, then
    1. Let C be Get(O, "constructor").
    2. ReturnIfAbrupt(C).
    3. If IsConstructor(C) is true, then
      1. Let thisRealm be the running execution context’s Realm.
      2. If thisRealm and the value of C’s Realm internal slot are the same value, then
        1. Let A be the result of calling the Construct internal method of C with an argument list containing the single item 0.
  10. If A is undefined, then
    1. Let A be the result of the abstract operation ArrayCreate with argument 0.
  11. ReturnIfAbrupt(A).
  12. Let k be 0.
  13. In arbitrary order for each array index value k between 0 and len - 1
    1. Let Pk be ToString(k).
    2. Let kPresent be the result of HasProperty(O, Pk).
    3. ReturnIfAbrupt(kPresent).
    4. If kPresent is true, then
      1. Let kValue be the result of Get(O, Pk).
      2. ReturnIfAbrupt(kValue).
      3. Let selected be the result of calling the Call internal method of callbackfun with T as thisArgument and a List containing kValue, k, and O as argumentsList.
      4. ReturnIfAbrupt(selected).
      5. If ToBoolean(selected) is true, then
        1. Let to be the number of selected values with indices less that pK
          (TBD – this might need a better explanation since in general one can’t determine without calling the callbackfun for all elements with lower indices.)
        2. Let status be the result of CreateDataPropertyOrThrow (A, ToString(to), kValue).
        3. ReturnIfAbrupt(status).
  14. Return A.

The length property of the filterPar method is 1.

NOTE Whether the filterPar function can be applied successfully to an exotic object that is not an Array is implementation-dependent.

Clone this wiki locally