在 Dart 中枚举或映射具有索引和值的列表 [英] Enumerate or map through a list with index and value in Dart

查看:22
本文介绍了在 Dart 中枚举或映射具有索引和值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 dart 中有任何等同于 common 的东西:

In dart there any equivalent to the common:

enumerate(List) -> Iterator((index, value) => f)
or 
List.enumerate()  -> Iterator((index, value) => f)
or 
List.map() -> Iterator((index, value) => f)

这似乎是最简单的方法,但这个功能不存在似乎仍然很奇怪.

It seems that this is the easiest way but it still seems strange that this functionality wouldn't exist.

Iterable<int>.generate(list.length).forEach( (index) => {
  newList.add(list[index], index)
});

推荐答案

有一个 asMap 方法可以将列表转换为映射,其中键是索引,值是索引处的元素.请查看此处的文档.

There is a asMap method which converts the list to a map where the keys are the index and values are the element at index. Please take a look at the docs here.

示例:

List _sample = ['a','b','c'];
_sample.asMap().forEach((index, value) => f);

希望这有帮助!

这篇关于在 Dart 中枚举或映射具有索引和值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆