Last active
December 30, 2020 09:37
-
-
Save milomai/c0f0387030c885457bb334ddfcda2154 to your computer and use it in GitHub Desktop.
[Dart] Map list with index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<int> list = [1, 2, 3]; | |
| List<String> result = list.asMap().entries.map((entry) { | |
| int idx = entry.key; | |
| int val = entry.value; | |
| return "$idx: $val"; | |
| }).toList(); | |
| // or | |
| List<String> result = list | |
| .asMap() | |
| .map((index, num) => MapEntry(index, "$index: $num")) | |
| .values | |
| .toList(); |
Author
Author
asMap() will keep the outter map's keys in numerical order, so the values will also follow this order.
Don't worry about the returnd list order.
ref: https://api.dart.dev/stable/dart-core/List/asMap.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref: https://fireship.io/snippets/dart-how-to-get-the-index-on-array-loop-map/