当调用映射函数在飞镖 /扑波中的地图对象上调用映射功能时,如何获取每个条目的索引?

发布于 2025-01-17 21:15:35 字数 504 浏览 2 评论 0原文

如何获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2025-01-24 21:15:35

您可以使用 forEach 和一个变量来跟踪当前索引:

Map<String, int> exampleMap = {"Spring": 1, "Chair": 9, "Autumn": 3};

void main() {
  int i = 0;
  exampleMap.forEach((key, value) {
    print(i.toString());
    print(key);
    print(value);
    i++;
  });
}

You can use forEach and a variable to track the current index:

Map<String, int> exampleMap = {"Spring": 1, "Chair": 9, "Autumn": 3};

void main() {
  int i = 0;
  exampleMap.forEach((key, value) {
    print(i.toString());
    print(key);
    print(value);
    i++;
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文