当调用映射函数在飞镖 /扑波中的地图对象上调用映射功能时,如何获取每个条目的索引?
如何获取Map
具体来说,如果在对象上运行映射函数(如下例所示),是否可以打印出每个条目的索引?
例如我怎样才能打印出: MapEntry(Spring: 1) 的索引为 0 MapEntry(主席: 9) 的索引为 1 MapEntry(Autumn: 3) 的索引为 2 等等。
Map<String, int> exampleMap = {"Spring" : 1, "Chair" : 9, "Autumn" : 3};
void main() {
exampleMap.entries.map((e) { return print(e);}).toList(); ///print each index here
}
-注意:我可以使用 List 对象获取索引(例如通过使用 exampleList.indexOf(e)),但不确定在使用 Map 对象时如何执行此操作。
How to get the index of each entry in a Map<K, V> in Dart?
Specifically, can the index of each entry be printed out, if a map function is run on the object as shown in the below example?
e.g. how could I print out:
MapEntry(Spring: 1) is index 0
MapEntry(Chair: 9) is index 1
MapEntry(Autumn: 3) is index 2
etc.
Map<String, int> exampleMap = {"Spring" : 1, "Chair" : 9, "Autumn" : 3};
void main() {
exampleMap.entries.map((e) { return print(e);}).toList(); ///print each index here
}
-Note: I can get the index with a List object (e.g. by using exampleList.indexOf(e)) but unsure how to do this when working with a Map object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
forEach
和一个变量来跟踪当前索引:You can use
forEach
and a variable to track the current index: