16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter DartでIndex付きのmapを簡単に行うExtension

Last updated at Posted at 2020-01-12

Extension

ちょいちょいmapをindex付きで使いたいことがあったので作りました。英語的にはEnumeratedMapのほうが正しいんだろうか…。

forEach のほうがメモリ食うけど高速かなと思いforEachで書いてます。

extension IndexedMap<T, E> on List<T> {
  List<E> indexedMap<E>(E Function(int index, T item) function) {
    final list = <E>[];
    asMap().forEach((index, element) {
      list.add(function(index, element));
    });
    return list;
  }
}

// example
final List<int> list = [1, 2, 3];
list.indexedMap((index, element) => index.toString() + element.toString());
16
11
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?