whereIndexedTo method

void whereIndexedTo(
  1. List<T> destination,
  2. bool predicate(
    1. T element,
    2. int index
    )
)

Appends all elements that satisfy the given predicate (which includes the element's index) to the destination list.

This function iterates over all elements in the iterable, alongside their indices. For each element, it checks if it satisfies the provided predicate function, which includes the element and its index. If it does, the element is added to the provided destination list.

Parameters: destination (List

Example:

List<String> words = ['apple', 'banana', 'apricot', 'avocado'];
List<String> aWords = [];
words.whereIndexedTo(aWords, (word, index) => word.startsWith('a') && index < 3);
// aWords list now contains ['apple', 'apricot']

Note: This function does not return a value. It modifies the destination list by adding elements to it. Ensure that the destination list is modifiable.

Implementation

void whereIndexedTo(List<T> destination, bool predicate(T element, int index)) {
  var index = 0;
  for (var element in this) {
    if (predicate(element, index++)) {
      destination.add(element);
    }
  }
}