Spec-Zone.ru › Dart 2

dart:collection

Метод indexWhere

int indexWhere(
  1. bool test(
    1. E element
    ),
  2. [int start = 0]
)
override

Индекс первого элемента в списке, удовлетворяющего заданному test.

Ищет в списке с индекса start до конца списка. В первый раз, когда встречается объект o, для которого test(o) истинно, возвращается индекс o.

final notes = <String>['do', 're', 'mi', 're'];
final first = notes.indexWhere((note) => note.startsWith('r')); // 1
final second = notes.indexWhere((note) => note.startsWith('r'), 2); // 3

Возвращает -1, если element не найден.

final notes = <String>['do', 're', 'mi', 're'];
final index = notes.indexWhere((note) => note.startsWith('k')); // -1

Реализация

int indexWhere(bool test(E element), [int start = 0]) {
  if (start < 0) start = 0;
  for (int i = start; i < this.length; i++) {
    if (test(this[i])) return i;
  }
  return -1;
}

© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-collection/ListMixin/indexWhere.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API