Array.prototype.filter()

차노·2023년 7월 28일
0

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.


  • filter(callbackFn)
  • filter(callbackFn, thisArg)

[Parameters]
callbackFn

  • A function to execute for each element in the array. It should return a truthy value to keep the element in the resulting array, and a falsy value otherwise. The function is called with the following arguments:

    element

  • The current element being processed in the array.

    index

  • The index of the current element being processed in the array.

    array

  • The array filter() was called upon.

    thisArg(optional)

  • A value to use as. this wehn executing callbackFn.

[Return value]
A shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned.

The filter() method is an iterative method. It calls a provided callbackFn function once for each element in an array, and constructs a new array of all the values for which callbackFn returns a truthy value. Array elements which do not pass the callbackFn test are not included in the new array. callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays. The filter() method is a copying method. It does not alter this but instead returns a shallow copy that contains the same elements as the ones from the original array (with some filtered out). However, the function provided as callbackFn can mutate the array. Note, however, that the length of the array is saved before the first invocation of callbackFn.

0개의 댓글