Enumerate or map through a list with index and value in Dart

后端 未结 8 2167
难免孤独
难免孤独 2020-12-08 01:28

In dart there any equivalent to the common:

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


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 02:17

    Lukas Renggli's more package includes many useful tools including 'indexed' which does exactly what you want. From the docs:

    indexed(['a', 'b'], offset: 1)
      .map((each) => '${each.index}: ${each.value}')
      .join(', ');
    

    (You can ignore the offset argument unless you have a Smalltalk background :-).

提交回复
热议问题